zac*_*ach 3 cocoa-touch objective-c ios
我试图保存/写一个数组到文件目录,我完全卡住了.我已经在线查看了多个来源的代码.我从和NSArray切换到NSMutableArray,但是没有用.据我所知,文件没有被写入,因为测试将返回null.提前致谢
NSArray *results = [parser objectWithData:data1];
NSMutableArray *array2 = [[NSMutableArray alloc] initWithArray:results];
// Create a dictionary from the JSON string
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentsDirectory = [paths objectAtIndex:0];
NSString *path = [documentsDirectory stringByAppendingPathComponent:@"EmployeeData.plist"];
//NSLog(@"the path is %@, the array is %@", path, array2);
/////write to file/////
[array2 writeToFile:path atomically:YES];
NSMutableArray *test = [[NSMutableArray alloc ]initWithContentsOfFile:path];
NSLog(@"the test is %@", test);
Run Code Online (Sandbox Code Playgroud)
**更新**
我添加了这些代码行
BOOL write = [results writeToFile:path atomically:YES];
NSLog(@"did it right? %@", write);
Run Code Online (Sandbox Code Playgroud)
并且写为null,这是什么意思?
这是我的Array中的众多对象之一
{
0 = "<null>";
1 = "John Doe";
10 = 512;
11 = "<null>";
12 = 1;
13 = 1;
2 = Doe;
3 = Jon;
4 = "<null>";
5 = johndoe;
6 = "<null>";
7 = "<null>";
8 = "john@doe.com";
9 = "<null>";
Type = 1;
Active = 1;
Depart = "<null>";
Emp = "<null>";
Ext = "<null>";
FirstName = Jim;
Fl = 512;
Name = "John Doe";
Log = jd;
Mobile = "<null>";
Title = "<null>";
}
Run Code Online (Sandbox Code Playgroud)
空值是否与它有关?
解决了!!
我意识到空值可能导致一些问题,所以我改变了我的查询以处理它并立即保存.上面的代码100%正确,它的工作原理!
谢谢您的帮助
aro*_*oth 16
按照该文件,writeToFile:只有工作,如果你的数组的内容仅限于类型屈指可数(NSString,NSData,NSArray,或NSDictionary对象).如果您的数组中有任何其他类型的对象,则它将无法正确写入文件(或根本不写入文件).
另请注意,writeToFile:返回一个布尔值,指示操作是否成功.
如果您的数组包含不兼容的类型,则必须使用类似NSKeyedArchiver的内容来序列化数据.