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

发布于 2024-11-04 07:29:05 字数 167 浏览 1 评论 0原文

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

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

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.

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.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

满地尘埃落定 2024-11-11 07:29:05
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];
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];
凉墨 2024-11-11 07:29:05

如果您打算不以编程方式创建 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.

这将添加到您的项目中。

在此处输入图像描述

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

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

故事还在继续 2024-11-11 07:29:05

我们可以对 plist 进行简单的了解,如下

在此处输入图像描述

现在您可以读取如下数据

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

We can get simple understanding about plist as below

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]);
怪我太投入 2024-11-11 07:29:05

创建新的 plist 文件 -

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

     [data writeToFile:PlistDataFilePath atomically:YES];

从此 plist 文件中读取数据 -

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

您应该阅读有关 plist 文件的这篇精彩的评论 - http://www.edumobile.org/iphone/iphone-programming-tutorials/how-to-use-plist-in-iphone/

create new plist file -

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 -

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/

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文