How can we create our own plist file in a Xcode pr

2019-01-17 19:14发布

问题:

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.

回答1:

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


回答2:

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.



回答3:

We can get simple understanding about plist as below

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


回答4:

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/