#import <Foundation/Foundation.h>
int main(int argc, const char * argv[])
{
@autoreleasepool {
NSArray * arr = @[ @"one", @"two", @"three", @"four", @"five", @"six" ];
// 回顾
NSString * path = @"/Users/stone/Desktop";
// @"/Users/stone/Desktop" ---> @"/Users/stone/Desktop/arr" ---> @"/Users/stone/Desktop/arr.plist"
path = [[path stringByAppendingPathComponent:@"arr"] stringByAppendingPathExtension:@"plist"];
NSLog(@"%@", path);
BOOL isSuccess = [arr writeToFile:path atomically:YES];
if (isSuccess) {
NSLog(@"writing success");
}
else {
NSLog(@"Write failed");
}
// 方法1: path
// NSArray * readArr = [NSArray arrayWithContentsOfFile:path];
// 方法2: url
NSURL * url = [NSURL URLWithString:[NSString stringWithFormat:@"file://%@", path]];
NSLog(@"%@", url);
NSArray * readArr = [NSArray arrayWithContentsOfURL:url];
NSLog(@"%@", readArr);
// 以 xml形式保存
NSString * xmlPath = @"/Users/stone/Desktop";
NSString * newPath = [xmlPath stringByAppendingFormat:@"%@",@"/arr.xml"];
[arr writeToFile:newPath atomically:YES];
NSArray * readArrXml = [NSArray arrayWithContentsOfFile:newPath];
NSLog(@"%@",readArrXml);
}
return 0;
}
NSArray 读写文件
最新推荐文章于 2020-04-23 21:11:30 发布