Using iOS SDK to create a plist at runtime?

半城伤御伤魂 提交于 2020-01-22 10:04:09

问题


I am new to iPhone development. I want to know if there is any sample Objective-C code to create a plist at runtimeby getting data from a webserver and I want to know what the format of the data should be so that I can easily create the plist at runtime.


回答1:


Plist files are mainly used by Mac OS X to store serialized objects in a key/value manner. There are multiple kinds of Property List files; ASCII, XML and Binary.So in your case your server should send the data in xml format.After receiving the data from server you can generate plist at runtime. You can use the below code to write data in .plist file.

- (void)writeToPlist{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"WebData.plist"];
NSArray *dataToSave = [dataToSave writeToFile:filePath atomically:YES];
}

Please refer this and these links for server side also link1 link2 link3 link4.




回答2:


Very simple with a NSDictionary:

#define DOCUMENTS_PATH [NSSearchPathForDirectoriesInDomains(NSDocumentsDirectory, NSUserDomainMask, YES) objectAtIndex:0]

NSDictionary *myDictionary = [self parseWebResult];

// note that myDictionary must only contain values of string, 
// int, bool, array (once again containing only the same types), 
// and other primitive types (I believe NSDates are valid too).
[myDictionary writeToFile:[DOCUMENTS_PATH stringByAppendingPathComponent:@"myDict.plist"] atomically:YES];

// reading in the dictionary
myDictionary = [NSDictionary dictionaryWithContentsOfFile:[DOCUMENTS_PATH stringByAppendingPathComponent:@"myDict.plist"]];



回答3:


// Get the full path of file in NSDocuments 
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *filePath = [documentsDirectory stringByAppendingPathComponent:@"MyData.plist"];

// Save data to file
NSArray *dataToSave = /* put data in it */;
[dataToSave writeToFile:filePath atomically:YES];


来源:https://stackoverflow.com/questions/8447341/using-ios-sdk-to-create-a-plist-at-runtime

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!