How to write the data to the plist

喜你入骨 提交于 2019-12-04 04:31:27

问题


HI can u send me sample code to add the data to the .plist. .my plist was in this format as follows.kindly help me out

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<array>
<string>http://localhost:8888/sample</string>
<string>http://localhost:8888/sample</string>
</array>
</plist>

i wil send the url string from the implementation code.kindly help me in this pls..

-(IBAction)AddFieldid)sender;
{
NSMutableArray *myPrimaryinfo = [[NSMutableArray arrayWithCapacity:6]retain];

NSArray *keys = [NSArray arrayWithObjects:@"string",nil];

[myPrimaryinfo addObject:[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:
[NSString stringWithFormat:@"sample"],nil]forKeys:keys]];


NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDire ctory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"urls.plist"];
[myPrimaryinfo writeToFile:path atomically:NO];
NSLog(@"PrimaryInfo array: %@", myPrimaryinfo);
}

Thanks


回答1:


I guess something like this would work (not very good at iphone stuff):

// Loading
NSString *path = ...
NSMutableArray *array = [[NSMutableArray alloc] initWithContentsOfFile:path];

// Manipulating
NSObject *newElement = ...
[array addObject:newElement];

// Storing
[array writeToFile:path atomically:YES];

By pressing "ALT" + double clicking on a class name opens up the docu for this class along with a list of methods and so on. Take a look there, too!



来源:https://stackoverflow.com/questions/3227094/how-to-write-the-data-to-the-plist

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