3

plistがあり、plistをxcodeプロジェクトにコピーしましたが、ファイルがバンドルされていないようです:

NSDictionary *dict = [[NSBundle mainBundle] infoDictionary];
nslog (@"dict %@",dict);

しかし、ファイル plist ファイルは表示されません。質問、バンドルで表示されるようにファイルをプロジェクトに追加するにはどうすればよいですか? また、plistを変更して変更を保存する方法を知っている人はいますか?

私は本当にあなたの助けに感謝します

PS私はXcode 5を使用しています。

4

3 に答える 3

4

バンドルに plist ファイルを作成し、その内容を次のように変更できます。

NSString *bundlePath = [[NSBundle mainBundle]bundlePath]; //Path of your bundle
NSString *path = [bundlePath stringByAppendingPathComponent:@"MyPlist.plist"]; 
NSFileManager *fileManager = [NSFileManager defaultManager];

NSMutableDictionary *data;

if ([fileManager fileExistsAtPath: path]) 
{
    data = [[NSMutableDictionary alloc] initWithContentsOfFile: path];  // if file exist at path initialise your dictionary with its data
}
else
{
    // If the file doesn’t exist, create an empty dictionary
    data = [[NSMutableDictionary alloc] init];
}

//To insert the data into the plist
int value = 5;
[data setObject:[NSNumber numberWithInt:value] forKey:@"value"];
[data writeToFile: path atomically:YES];
[data release];

//To retrieve the data from the plist
NSMutableDictionary *savedData = [[NSMutableDictionary alloc] initWithContentsOfFile: path];
int savedValue;
savedValue = [[savedData objectForKey:@"value"] intValue];
NSLog(@"%i",savedValue);
于 2013-10-28T10:37:42.950 に答える