开发者

Objective c : problem with directory file

I have this code:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringBy开发者_StackOverflow社区AppendingPathComponent:@"File.plist"]; 


if([array writeToFile:path atomically: YES]){
    NSLog(@"write succesful");}
else {
    NSLog(@"write failed");
}

I create in folder Resources a file "File.plist" and I want store an NSMutableArray inside it. When I call this method the message in console is "write succesful" but if I change the path in

NSString *path = [documentsDirectory stringByAppendingPathComponent:@"FileAbstract.plist"];

the message is ever "write succesful? Why? I changed name in path.


In this code you are writing in document directory, which has no relation with the resource folder in the project. You cannot change files in resource folders by running the app. In first run you have written file File.plist and in 2nd run you have written file FileAbstract.plist, both in document directory. They have no relation with the File.plist in resource folder.

EDIT: To read the content of the file in an array:

NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"File.plist"];

NSArray *array = [NSArray arrayWithContentsOfFile:path];
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜