0

.plist を読みたいのですが、NSLog が "(null)" に等しいです。これが私のコードです:

NSMutableDictionary* plistDict = [[NSMutableDictionary alloc] initWithContentsOfFile:@"data.plist"];

NSLog(@"%@", [plistDict objectForKey:@"id"]);

そして、ここに私の .plist があります: http://cl.ly/image/2e3P3Q1z2Z1T

4

1 に答える 1

2

あなたの道は間違っています。plist がアプリ リソースにある場合は、NSBundle絶対パスを取得するために使用する必要があります。

NSString *path = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"];
NSMutableDictionary *dict = [[NSDictionary dictionaryWithContentsOfFile:path] 
                              mutableCopy];

アプリのドキュメント フォルダーにある場合は、次の操作を実行できます。

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *basePath = ([paths count] > 0) ? [paths objectAtIndex:0] : nil;
NSString *path = [basePath stringByAppendingPathComponent:@"data.plist"];
NSMutableDictionary *dict = [[NSDictionary dictionaryWithContentsOfFile:path] mutableCopy];

plist からロードされた辞書または配列は常に不変であることに注意してください。変更可能なコピーを作成する必要があります。

于 2012-10-28T20:15:00.210 に答える