2010-10-04 14 views
0

我使用Fetch查詢sqlite數據庫中的行,並將結果加載到NSMutableArray myDataArray中。我想要做的是將myDataArray中的值存儲到我的應用程序的首選項plist文件中。但是,當我運行下面的代碼,我得到以下錯誤:如何在應用程序的plist文件中存儲提取結果?

[NSUserDefaults的方法setObject:forKey:]:嘗試插入非財產價值「( ...(注:包含50個行值) )」類'__NSArrayM'。

我使用相同的方法在我的plist文件中存儲其他NSMutableArrays,但無法找出存儲查詢的數據庫結果的方法。

您可能會問:爲什麼要存儲這些值,只是在需要再次查詢數據庫時重新查詢數據庫?原因是我從數據庫中隨機抽取了行,我需要回憶這些行與它們最初的隨機數。

任何幫助表示讚賞, LQ

... 
fetchResults = [managedObjectContext executeFetchRequest:request error:&error]; 
self.myDataArray = [NSMutableArray arrayWithArray:fetchResults]; 

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefaults]; 
[userDefaults setObject:self.myDataArray forKey:kFetchResultsArray]; 

回答

0

在myDataArray的項目是不是屬性列表對象,所以它們不能被自動序列化到一個plist中。

NSUserDefaults class referencesetObject:forKey:

The value parameter can be only property list objects: NSData, NSString, NSNumber, NSDate, NSArray, or NSDictionary. For NSArray and NSDictionary objects, their contents must be property list objects. See 「What is a Property List?」 in Property List Programming Guide.

你可能要考慮比用戶默認存儲該數據的其它機制。來自Property List Programming Guide

Many applications require a mechanism for storing information that will be needed at a later time. For situations where you need to store small amounts of persistent data—say less than a few hundred kilobytes—property lists offer a uniform and convenient means of organizing, storing, and accessing the data.

In some situations, the property-list architecture may prove insufficient. If you need a way to store large, complex graphs of objects, objects not supported by the property-list architecture, or objects whose mutability settings must be retained, use archiving. See Archives and Serializations Programming Guide for more information.

+0

感謝您的快速響應。使用存檔對我來說似乎不是一個好的解決方案,所以我正在考慮的是重新設計應用程序,以便將所有50行數據拖入myDataArray,而不是將PK_ID拉入數組,將它們存儲在plist中,然後使用Predicate每次取一行來調用每個PK_ID。 – 2010-10-04 21:06:07

相關問題