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

时间:2011-05-02 05:29:28

标签: iphone xcode dictionary plist

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

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

4 个答案:

答案 0 :(得分: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];

答案 1 :(得分: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.

这会添加到您的项目中。

enter image description here

答案 2 :(得分:3)

我们可以通过以下方式简单了解plist

enter image description here

现在您可以阅读以下数据

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

答案 3 :(得分:1)

创建新的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/