如何在NSUserDefault中存储自定义对象
NSUserDefaults
-[NSUserDefaults setObject:forKey:]: Attempt to insert non-property value '<Player: 0x3b0cc90>' of class 'Player'.
NSUserDefaults
NSUserDefaults
NSUSerDefaults
?
@interface Player : NSObject { NSString *name; NSNumber *life; //Log of player's life}//Getting functions, return the info- (NSString *)name;- (int)life;- (id)init;//These are the setters- (void)setName:(NSString *)input; //string- (void)setLife:(NSNumber *)input; //number @end
#import "Player.h"@implementation Player- (id)init { if (self = [super init]) { [self setName:@"Player Name"]; [self setLife:[NSNumber numberWithInt:20]]; [self setPsnCounters:[NSNumber numberWithInt:0]]; } return self;}- (NSString *)name {return name;}- (int)life {return [life intValue];}- (void)setName:(NSString *)input { [input retain]; if (name != nil) { [name release]; } name = input;}- (void)setLife:(NSNumber *)input { [input retain]; if (life != nil) { [life release]; } life = input;}/* This code has been added to support encoding and decoding my objecst */-(void)encodeWithCoder:(NSCoder *)encoder{ //Encode the properties of the object [encoder encodeObject:self.name forKey:@"name"]; [encoder encodeObject:self.life forKey:@"life"];}-(id)initWithCoder:(NSCoder *)decoder{ self = [super init]; if ( self != nil ) { //decode the properties self.name = [decoder decodeObjectForKey:@"name"]; self.life = [decoder decodeObjectForKey:@"life"]; }
慕的地6264312
相关分类