2013-02-22 33 views
1

所以我有一個需要保存到文件的對象的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]; 

我在這裏錯過了什麼?

+1

使用NSKeyed(Un)Archiver。來自UTF8編碼的腐敗風險太高。 – CodaFi 2013-02-22 16:16:01

+0

你失去了我,你能解釋一下嗎? – Slee 2013-02-22 16:18:15

+1

保存/恢復爲JSON。 JSON將很好地處理NSNull對象。 – 2013-02-22 16:24:34

回答

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

initWithContentsOfFile將路徑作爲字符串。在你的情況下,它不是路徑,而是內容本身。

+0

但問題依然存在'[NSNull null]'。 – 2013-02-22 17:37:08

+0

不適合我,這就像一個冠軍 – Slee 2013-02-23 00:24:31

1

我看到的是你的字符串實際上是CoreData'sNSManagedObject(orders arrays)的描述。因此,您可以將字符串保存到文件中,但不能從該文件重新創建相同的對象。這只是NSManagedObject上的-description方法的結果。如果這是您要實現的目標,則必須實施保存對象數據的定製邏輯(否則,其中,而不是您的CoreData's持久性存儲,以及將加載該數據的邏輯,將其插入NSManagedObjectContext中)。

0

[NSString stringWithFormat:@"%@",orders]只是給你調用[orders description]的結果,它只是爲了調試而不是歸檔它。

你可以只調用將writeToFile:原子:你orders陣列上,然後做[[NSArray alloc] initWithContentsOfFile:filePath]讀它回來了,有一點需要注意:

如果陣列中的內容都是屬性列表對象(的NSString,NSData的,NSArray或NSDictionary對象),則可以使用此方法編寫的文件用類方法arrayWithContentsOfFile:或實例方法initWithContentsOfFile:來初始化新數組。此方法在寫出文件之前遞歸地驗證所有包含的對象是屬性列表對象,並且如果所有對象都不是屬性列表對象,則返回NO,因爲生成的文件不是有效的屬性列表。

所以數組中的對象必須都是NSString,NSData,NSArray或NSDictionary類才能使其工作。