我们如何在Xcode项目中创建自己的plist文件?

Ana*_*and 3 iphone xcode dictionary plist

我想在我的应用程序中创建"UrlPaths.plist"文件,还有一个包含4或5个对象的字典.请帮我创建一个plist文件和字典.并且还从该plist文件中读取数据.

我希望plist将文件添加到资源文件夹,我想在那时添加词典.我不想要实用的plist创建,但我想要实用地阅读数据.

Ara*_*han 11

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 
NSString *documentsDirectory = [paths objectAtIndex:0]; 
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"plist.plist"]; 
NSFileManager *fileManager = [NSFileManager defaultManager];

NSMutableDictionary *data;

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

//To insert the data into the plist
data[@"value"] = @(5);
[data writeToFile: path atomically:YES];
[data release];

//To retrieve the data from the plist
NSMutableDictionary *savedStock = [[NSMutableDictionary alloc] initWithContentsOfFile:path];
int value1;
value1 = [savedStock[@"value"] intValue];
NSLog(@"%i",value1);
[savedStock release];
Run Code Online (Sandbox Code Playgroud)


Jay*_*bey 8

如果您要以编程方式创建Plist,请按照以下步骤操作:

1. Right Click on Files in Left Pane and Select 'New File...' option.
2. Choose Resources from OS X tab.
3. An option for Property List is available.
4. Select an give an appropriate name.
Run Code Online (Sandbox Code Playgroud)

这会添加到您的项目中.

在此输入图像描述