错误写给plist

时间:2011-11-29 11:12:06

标签: iphone objective-c xcode

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

这一行给出了找不到方法-stringByAppendingPathComponent的错误。问题是什么

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

3 个答案:

答案 0 :(得分:4)

正如Mats在评论中指出的那样,但似乎被忽略了(或者在编辑中被你错误地纠正):

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

应该是

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

注意括号的位置。

答案 1 :(得分:3)

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

NSString *documentsDirectory = [paths objectAtIndex:0]; 

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

然后

[array writeToFile:path atomically:YES]; 

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

如果仍未创建plist,请检查您的数组或字典是否必须为空。

答案 2 :(得分:0)

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