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

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/5853735/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-09 03:55:18  来源:igfitidea点击:

How can we create our own plist file in a Xcode project?

iphonexcodedictionaryplist

提问by Anand

I want to create "UrlPaths.plist" file in my Application and also a dictionary with 4 or 5 objects. Please help me create a plist file and dictionary. And also read data from that plist file.

我想在我的应用程序中创建“UrlPaths.plist”文件以及一个包含 4 或 5 个对象的字典。请帮我创建一个 plist 文件和字典。并从该 plist 文件中读取数据。

I want the plist to add the file to resources folder and i want to add Dictionary at that time also.i dont want pragmatical creation of plist but i want reading the data is pragmatically.

我希望 plist 将文件添加到资源文件夹中,并且当时我还想添加 Dictionary。我不想实用地创建 plist,但我想实用地读取数据。

回答by Aravindhan

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];

回答by Jayprakash Dubey

If you are about to create Plist without programmatically then follow these steps :

如果您要在没有编程的情况下创建 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.

This gets added to your project.

这将添加到您的项目中。

enter image description here

在此处输入图片说明

回答by sinh99

We can get simple understanding about plist as below

我们可以简单的了解 plist 如下

enter image description here

在此处输入图片说明

Now you can read this data as below

现在你可以阅读这些数据如下

NSString *path = [[NSBundle mainBundle] pathForResource:@"Priority" ofType:@"plist"];
NSDictionary *dictPri = [NSDictionary dictionaryWithContentsOfFile:path];//mm
NSMutableArray *arrMarkets=[[NSMutableArray alloc] initWithArray:[dictPri valueForKey:@"List"]];
NSMutableArray *arrForTable=[[NSMutableArray alloc] init];
NSMutableArray *arrForTable1=[[NSMutableArray alloc] init];
for (NSDictionary *dict in arrMarkets)
{
    NSString *strS1=nil;
    strS1= [NSString stringWithFormat:@"%@",[dict valueForKey:@"Description"] ];
    [arrForTable addObject:strS1];
}
NSLog(@"%@----------------- ",[arrForTable description]);
for (NSDictionary *dict in arrMarkets)
{
    NSString *strS2=nil;
    strS2= [NSString stringWithFormat:@"%@",[dict valueForKey:@"Name"] ];
    [arrForTable1 addObject:strS2];
}    
NSLog(@"%@----------------- ",[arrForTable1 description]);

回答by Saurabh

create new plist file -

创建新的 plist 文件 -

NSArray *Arr = [NSArray arrayWithObjects:obj1,obj2,nil];
        NSData *data = [NSPropertyListSerialization dataFromPropertyList:Arr  format:NSPropertyListXMLFormat_v1_0  errorDescription:nil];

     [data writeToFile:PlistDataFilePath atomically:YES];

Read data from this plist file -

从这个 plist 文件中读取数据 -

NSData *data = [NSData dataWithContentsOfFile:PlistDataFilePath];
NSPropertyListFormat format;
NSArray *array = [NSPropertyListSerialization propertyListFromData:data mutabilityOption:NSPropertyListImmutable format:&format errorDescription:nil];

you should read this great tut on plist files - http://www.edumobile.org/iphone/iphone-programming-tutorials/how-to-use-plist-in-iphone/

你应该阅读这个关于 plist 文件的很棒的 tut - http://www.edumobile.org/iphone/iphone-programming-tutorials/how-to-use-plist-in-iphone/