使用iOS SDK在运行时创buildplist?

我是iPhone开发新手。 我想知道是否有任何示例Objective-C代码在运行时通过从Web服务器获取数据来创buildplist,我想知道数据的格式应该是什么,以便我可以在运行时轻松创buildplist。

Plist文件主要由Mac OS X用键/值方式存储序列化的对象。 有多种属性列表文件; ASCII,XML和Binary.So在你的情况下,你的服务器应该以xml格式发送数据。从服务器接收到数据后,你可以在运行时生成plist。 您可以使用下面的代码在.plist文件中写入数据。

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

请参考这个链接和服务器端的链接link1 link2 link3 link4 。

用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"]]; 
 // 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];