How to write the data to the plist

2019-02-25 06:25发布

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条回答
走好不送
2楼-- · 2019-02-25 06:33

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!

查看更多
登录 后发表回答