将NSArray写入文件 [英] Writing a NSArray to file

查看:156
本文介绍了将NSArray写入文件的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图保存/写一个数组到文件目录,我完全卡住了。我已经检查这个代码与多个来源在线。我从和NSArray切换到一个NSMutableArray,并没有工作。从我可以告诉的是文件没有被写,因为测试回到null。提前感谢

  NSArray * results = [parser objectWithData:data1]; 

NSMutableArray * array2 = [[NSMutableArray alloc] initWithArray:results];
//从JSON字符串创建字典

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

// NSLog(@路径为%@,数组为%@,path,array2);

/////写入文件/////
[array2 writeToFile:path atomically:YES];

NSMutableArray * test = [[NSMutableArray alloc] initWithContentsOfFile:path];

NSLog(@测试为%@,测试);

**更新**



添加这些代码行

  BOOL write = [results writeToFile:path atomically:YES]; 

NSLog(@did not right?%@,write);

并且写为null,这是什么意思?



这是我的数组中的许多对象之一

  {
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>;
}

空值与它有什么关系吗?



解决了!



我意识到空值可能导致一些问题,所以我改变了我的查询来照顾它和它立即保存。

解决方案

根据文档 writeToFile:只有在你的数组的内容被限制为少数类型时才有效( NSString NSData NSArray NSDictionary 对象)。如果你的数组中有任何其他类型的对象,那么它不会正确地写入文件。



注意 writeToFile:返回一个布尔值,表示操作是否成功。



如果您的数组包含不兼容的类型,您必须查看使用 NSKeyedArchiver 来序列化您的数据。


I am trying to save/write an Array to the documents directory, and I am completely stuck. I have checked this code with multiple sources online. I switched from and NSArray to a NSMutableArray and that didn't work. From what I can tell is the file isn't being written, because test is coming back null. Thanks in advance

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

** update **

I added these lines of code

BOOL write = [results writeToFile:path atomically:YES];

NSLog(@"did it right? %@", write);

and write is null, what does that mean?

This is one of the many objects in my 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>";
}

Do the null values have anything to do with it?

SOLVED!!

I realized that the null values may have been causing some issues, so i changed my query to take care of that and it saved instantly. The above code is 100% correct and it works!

thanks for the help

解决方案

As per the documentation, writeToFile: only works if your array's contents are limited to a handful of types (NSString, NSData, NSArray, or NSDictionary objects). If you have any other type of object in your array, then it will not write to file correctly (or at all).

Note also that writeToFile: returns a boolean value indicating whether or not the operation succeeded.

If your array contains incompatible types, you will have to look at using something like NSKeyedArchiver to serialize your data instead.

这篇关于将NSArray写入文件的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆