How to write data to plist

HI you can send me a sample code for adding data to .plist..my plist was in this format as follows. Somehow help me

<?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 sends the url string from the implementation code. Anyway, help me on 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);
}

thank

+3
source share
1 answer

I think something like this will work (not very good for iphone):

// 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 the class name opens a document for this class, as well as a list of methods, etc. Look there too!

+1
source

Source: https://habr.com/ru/post/1754202/


All Articles