简体   繁体   中英

error writing to plist

[array writeToFile:[documentsDirectory stringByAppendingPathComponent:@"data.plist" atomically:YES];

this line gives error that method -stringByAppendingPathComponent not found. whats the issue

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSArray *array = [[NSArray alloc]initWithObjects:@"First", @"Second", @"Third", nil];
[array writeToFile:[documentsDirectory stringByAppendingPathComponent:@"data.plist" atomically:YES]];

As pointed out by Mats in comments, but seemingly ignored (or incorrectly corrected by you in the edit):

[array writeToFile:[documentsDirectory stringByAppendingPathComponent:@"data.plist" atomically:YES]]; 

Should be

[array writeToFile:[documentsDirectory stringByAppendingPathComponent:@"data.plist"] atomically:YES]; 

Note the positioning of the brackets.

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); 

NSString *documentsDirectory = [paths objectAtIndex:0]; 

plistpath = [[NSString alloc] initWithString:[documentsDirectory stringByAppendingPathComponent:@"Product.plist"]];

And then

[array writeToFile:path atomically:YES]; 

[Whatever [Array or Dictionary] you want to Write in Plist]

If it is still not creating the plist then check whether your array or dictionary must be empty.

    NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
    NSString *documentsDirectory = [paths objectAtIndex:0];
    NSLog(@"%@",documentsDirectory);

    NSString *plistName = @"%@chores.plist";
    NSString *path = [documentsDirectory stringByAppendingPathComponent:plistName];

    NSArray *array=[[NSArray alloc]initWithObjects:@"abc",@"pqr",@"xyz",nil];
[array writeToFile:path atomically:YES];

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM