1

所以我有一个 NSMutableArray 需要保存到文件的对象。用方法保存它writeToFile不起作用,因为我有 NSNull 值,这些值在此方法生成的 .plist 格式中是不允许的。

所以我通过将文件保存为 NSString 来将文件保存到磁盘:

NSString *content = [NSString stringWithFormat:@"%@",orders];
[content writeToFile:ordersFilePath atomically:NO];

这会生成一个像这样的文件:

(
        {
        ZBILLINGADDRESS1 = "";
        ZBILLINGADDRESS2 = "";
        ZBILLINGCITY = "";
        ZBILLINGCOUNTRY = "";
        ZBILLINGSTATE = "";
        ZBILLINGZIP = "";
        ZCANCELDATE = "<null>";
        ZCATALOGCODE = "";
        ZCATALOGNAME = "";
        ZCONTACTEMAIL = "";
        ZCONTACTFAX = "";
        ZCONTACTNAME = " ";
        ZCONTACTPHONE = "";
        ZCONTAINERCUBES = 0;
        ZCONTAINERNAME = "";
        ZCURRENCYCODE = "<null>";
        ZCUSTOMERID = 0;
        ZCUSTOMERNAME = "Dupe offline Test";
        ZCUSTOMERNUMBER = 1297145;
        ZDISCOUNT = 0;
        ZDONOTSHIPBEFORE = "<null>";
        ZEXPORTDATE = "-63113886238";
        ZEXPORTED = 1;
        ZFOB = "";
        ZFREIGHTTERM = "";
        ZISCONTAINER = 0;
        ZISDEFAULT = 0;
        ZISDELETED = 0;
        ZISPLACED = 0;
        ZMANUFACTURERID = 18;
        ZMARKETNAME = "";
        ZNOTES = "last second note";
        ZORDERDATE = 372571200;
        ZORDERGUID = "5650d30a-9316-436a-87e9-1e0ead3af8d7";
        ZORDERID = 84340;
        ZORDERSTATUS = Accept;
        ZORDERTOTAL = 200;
        ZORDERTYPE = "";
        ZORIGIN = "";
        ZPONUMBER = 3;
        ZPRICELEVEL = 2;
        ZREPNUMBER = 1;
        ZSALESREPID = 235;
        ZSHIPDATE = 373176000;
        ZSHIPPINGTOTAL = 0;
        ZSHIPTOADDRESS1 = "";
        ZSHIPTOADDRESS2 = "";
        ZSHIPTOCITY = "";
        ZSHIPTOCOMPANYNAME = "";
        ZSHIPTOCONTACTEMAIL = "";
        ZSHIPTOCONTACTNAME = "";
        ZSHIPTOCONTACTPHONE = "";
        ZSHIPTOCOUNTRY = "";
        ZSHIPTOFAX = "";
        ZSHIPTOSTATE = "";
        ZSHIPTOZIP = "";
        ZSHIPVIA = "";
        ZSOURCE = ipad;
        ZSUBTOTAL = 200;
        ZTAXRATE = 0;
        ZTAXTOTAL = 0;
        ZTERMS = "";
        ZWRITTENBYNAME = "";
        ZWRITTENFORNAME = "";
        ZWRITTENFORREPNUMBER = "";
        ZWRITTENFORREPNUMBER1 = "<null>";
        "Z_ENT" = 17;
        "Z_OPT" = 5;
        "Z_PK" = 450;
    }
)

问题是当我尝试将其重新加载为 NSArray 时,我得到一个空值:

NSString *str = [NSString stringWithContentsOfFile:ordersFilePath encoding:NSUTF8StringEncoding error:NULL];
    NSArray *loadedOrders = [[NSArray alloc] initWithContentsOfFile:str];

我在这里想念什么?

4

3 回答 3

2
NSArray *loadedOrders = [[NSArray alloc] initWithContentsOfFile:str];

initWithContentsOfFile 将路径作为字符串。在您的情况下,它不是路径而是内容本身。

于 2013-02-22T16:54:57.547 回答
1

我看到的是您的字符串实际上是CoreData's NSManagedObject(订单数组)的描述。因此,您可以将字符串保存到文件中,但不能从该文件重新创建相同的对象。这只是-description你的方法的结果NSManagedObject。如果这是您想要实现的,您必须实现保存对象数据的自定义逻辑(否则,不是您的CoreData's持久存储,以及将加载该数据的逻辑,通过将其插入 a NSManagedObjectContext)。

于 2013-02-22T16:20:38.417 回答
0

[NSString stringWithFormat:@"%@",orders]只是给你调用 [orders description] 的结果,这只是为了调试而不是存档。

您可以只调用 writeToFile:atomically: 在您的orders数组上,然后将[[NSArray alloc] initWithContentsOfFile:filePath]其读回,有一个警告:

如果数组的内容都是属性列表对象(NSString, NSData, NSArray, 或 NSDictionary 对象),这个方法写入的文件可以通过类方法arrayWithContentsOfFile: 或者实例方法initWithContentsOfFile: 来初始化一个新数组。此方法在写出文件之前递归地验证所有包含的对象都是属性列表对象,如果所有对象都不是属性列表对象,则返回 NO,因为结果文件不是有效的属性列表。

所以数组中的对象都必须是 NSString、NSData、NSArray 或 NSDictionary 类才能正常工作。

于 2013-02-22T17:06:48.987 回答