所以我有一個需要保存到文件的對象的NSMutableArray。使用writeToFile
方法保存它將不起作用,因爲我有NSNull值,而此方法生成的.plist格式不允許。加載NSArray作爲NSString保存到文件,因爲我有NSNull值
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;
}
)
問題是,當我嘗試重新加載此作爲
所以我被保存爲一個的NSString救了我的文件保存到磁盤NSArray我得到一個空值:
NSString *str = [NSString stringWithContentsOfFile:ordersFilePath encoding:NSUTF8StringEncoding error:NULL];
NSArray *loadedOrders = [[NSArray alloc] initWithContentsOfFile:str];
我在這裏錯過了什麼?
使用NSKeyed(Un)Archiver。來自UTF8編碼的腐敗風險太高。 – CodaFi 2013-02-22 16:16:01
你失去了我,你能解釋一下嗎? – Slee 2013-02-22 16:18:15
保存/恢復爲JSON。 JSON將很好地處理NSNull對象。 – 2013-02-22 16:24:34