に配列を保存しようとしていNSUserDefaults
ます。エンコーダーとデコーダーを使用する必要があることを読みましたが、それでも同じエラーが発生します
*** -[NSUserDefaults setObject:forKey:]: Attempt to insert non-property value '(
"<Item: 0x1e86e380>",
"<Item: 0x1e86fd60>",
"<Item: 0x1e86fda0>",
"<Item: 0x1e86fde0>",
"<Item: 0x1e86fe10>"
)' of class '__NSArrayM'. Note that dictionaries and arrays in property lists must also contain only property values.
[unknown][unknown][unknown][unknown][unknown](gdb)
どうして??
これが私のコードです:
これは私のモデルです。NSStrings
、NSNumbers
およびNSMutableArrays
: _
- (void)encodeWithCoder:(NSCoder *)encoder
{
[encoder encodeObject: self.title forKey:@"title"];
[encoder encodeObject: self.desc forKey:@"desc"];
[encoder encodeObject: (NSNumber*) self.day forKey:@"day"];
[encoder encodeObject: self.images forKey:@"images"];
}
- (id)initWithCoder:(NSCoder *)decoder
{
self = [super init];
if( self != nil )
{
self.title = [decoder decodeObjectForKey: @"title"];
self.desc = [decoder decodeObjectForKey: @"desc"];
self.day = [decoder decodeObjectForKey: @"day"];
self.images = [decoder decodeObjectForKey: @"images"];
}
return self;
}
これは AppDelegate のDidFinishLaunching
メソッドにあります。
if([[NSUserDefaults standardUserDefaults] objectForKey:@"savedData"] == nil)
{
items = [[NSMutableArray alloc] init];
NSString *filePath = [[NSBundle mainBundle] pathForResource:@"items" ofType:@"json"];
NSString *contents = [NSString stringWithContentsOfFile: filePath encoding: NSUTF8StringEncoding error: nil];
SBJsonParser *jsonParser = [[SBJsonParser alloc] init];
NSMutableDictionary *json = [jsonParser objectWithString: contents];
jsonParser = nil;
items = json[@"item"];
for(NSDictionary *itemDict in items)
{
Item *item = [[Item alloc] init];
item.title = [itemDict valueForKey:@"title"];
item.desc = [itemDict valueForKey:@"description"];
item.day = [itemDict valueForKey:@"day"];
item.images = [itemDict valueForKey: @"images"];
[items addObject: item];
}
}
else
{
NSData *decodedObject = [userDefault objectForKey: [NSString stringWithFormat:@"savedData"]];
NSArray *decodedArray =[NSKeyedUnarchiver unarchiveObjectWithData: decodedObject];
items = [decodedArray mutableCopy];
}
そして、これは私が持っていAppWillTerminate
ます:
userDefault = [NSUserDefaults standardUserDefaults];
NSData *encodedObject = [NSKeyedArchiver archivedDataWithRootObject: items];
[userDefault setObject: encodedObject forKey:[NSString stringWithFormat:@"savedData"]];