如何在NSUserDefault中存储自定义对象好吧,所以我已经做了一些周旋,我意识到我的问题,但我不知道如何解决它。我做了一个自定义类来保存一些数据。我为这个类创建对象,并且需要它们在会话之间持续。在我把所有信息都放进去之前NSUserDefaults但这不管用。-[NSUserDefaults setObject:forKey:]: Attempt to insert non-property value '<Player: 0x3b0cc90>' of class 'Player'.当我将自定义类“Player”放入NSUserDefaults..显然,我已经读过了NSUserDefaults只存储某些类型的信息。所以我如何让我的对象NSUSerDefaults?我读到应该有一种方法来“编码”我的自定义对象,然后把它放进去,但是我不知道如何实现它,帮助是非常感谢的!谢谢!*编辑*好的,所以我使用了下面给出的代码(谢谢!),但是我仍然有一些问题。基本上,代码现在崩溃了,我不知道为什么,因为它不会产生任何错误。也许我错过了一些基本的东西,我只是太累了,但我们会看到。以下是我的自定义类“Player”的实现:@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"];
}抱歉这么多密码。只是想帮忙。基本上,该应用程序将启动,然后立即崩溃。我已经把它缩小到了应用程序的加密部分,那是它崩溃的地方,所以我做了一些错误的事情,但我不确定是什么。再次感谢您的帮助,谢谢!(我还没来得及解密,因为我还没有开始加密。)
3 回答
慕的地6264312
TA贡献1817条经验 获得超6个赞
- (void)encodeWithCoder:(NSCoder *)encoder { //Encode properties, other class variables, etc [encoder encodeObject:self.question forKey:@"question"]; [encoder encodeObject:self.categoryName forKey:@"category"]; [encoder encodeObject:self.subCategoryName forKey:@"subcategory"];}- (id)initWithCoder:(NSCoder *)decoder { if((self = [super init])) { //decode properties, other class vars self.question = [decoder decodeObjectForKey:@"question"]; self.categoryName = [decoder decodeObjectForKey:@"category"]; self.subCategoryName = [decoder decodeObjectForKey:@"subcategory"]; } return self;}
NSUserDefaults
:
- (void)saveCustomObject:(MyObject *)object key:(NSString *)key { NSData *encodedObject = [NSKeyedArchiver archivedDataWithRootObject:object]; NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; [defaults setObject:encodedObject forKey:key]; [defaults synchronize];}- (MyObject *)loadCustomObjectWithKey:(NSString *)key { NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; NSData *encodedObject = [defaults objectForKey:key]; MyObject *object = [NSKeyedUnarchiver unarchiveObjectWithData:encodedObject]; return object;}
- 3 回答
- 0 关注
- 718 浏览
添加回答
举报
0/150
提交
取消