2011-08-05 9 views
11

を返します。 、それはアプリのバンドルでplistを見つけることができないことを示唆)。のアクセスは、私は配列にPLISTをロードするために、成功して前に使用したコードの一部を使用していヌル

iPhoneシミュレータの下では、最初の行で作成されたパスがアプリファイルを指していることを確認しました。Finderのコンテキストメニュー「Show package contants」を使用して、「species_name.plist実際にバンドルされています。長さは内容が含まれていることを示していますので、でなければなりません(私が書いた他のiOSアプリケーションにもあります)。提案は大歓迎です...

[env Xcode 4.2 beta、iOS 4.3.2]

答えて

27

ではなくNSDictionaryインスタンスのinitWithContentsOfFile:メソッドを使用する必要があります。

NSString *filePath = [[NSBundle mainBundle] pathForResource:@"species_names" ofType:@"plist"]; 
NSDictionary *dict = [[NSDictionary alloc] initWithContentsOfFile:filePath]; 
NSArray *array = [NSArray arrayWithArray:[dict objectForKey:@"Root"]]; 

またはこの:あなたはNSArraywriteToFile:atomically:方法で作成したファイルをNSArrayinitWithContentsOfFile:メソッドを使用することができます

NSString *errorDesc = nil; 
NSPropertyListFormat format; 
NSString *path = [[NSBundle mainBundle] pathForResource:@"startups" ofType:@"plist"]; 
NSData  *plistXML = [[NSFileManager defaultManager] contentsAtPath:path]; 
NSDictionary *temp = (NSDictionary *)[NSPropertyListSerialization      
              propertyListFromData:plistXML 
mutabilityOption:NSPropertyListMutableContainersAndLeaves 
              format:&format 
              errorDescription:&errorDesc]; 
NSArray *array = [NSArray arrayWithArray:[temp objectForKey:@"Root"]]; 


EDITここ はサンプルです。 writeToFile:atomically:で作成されたplistはこのstuctureがあります

<plist version="1.0"> 
<array> 
    ...Content... 
</array> 
</plist> 

そして、PLISTはXcodeで作成したが、このstuctureがあります

<plist version="1.0"> 
<dict> 
    ...Content... 
</dict> 
</plist> 

ザッツなぜNSArray dosnt仕事のinitWithContentsOfFile:方法。このタイプの

+0

トラックに私を得ました。ありがとう。 – RonC

2

plist

enter image description here

OR(基本的に両方とも同じです)

enter image description here

は、次のように読むことができます:

NSString *file = [[NSBundle mainBundle] pathForResource:@"appFeatures" ofType:@"plist"]; 
NSArray *_arrFeats = [NSArray arrayWithContentsOfFile:file]; 
NSLog(@"%d", _arrFeats.count); 

appFeaturesplistの名前です。 (iOSの6.1)

0

この単純なmetod試してみてください:

- (void)viewDidLoad 
{ 
    [super viewDidLoad]; 
    [self plistData]; 
} 


- (void)plistData 
{ 
    NSString *file = [[NSBundle mainBundle] pathForResource:@"appFeatures" ofType:@"plist"]; 
    NSArray *messages = [NSArray arrayWithContentsOfFile:file]; 
    NSLog(@"%d", messages.count); 

    for (int i=0; i<messages.count; i++) 
    { 
     NSString *data = [messages objectAtIndex:i]; 
     NSLog(@"\n Data = %@",data); 
    } 
} 
関連する問題