保存自己的数据类型时使用NSUserDefaults

时间:2013-08-29 11:37:40

标签: ios objective-c nsuserdefaults

我已经阅读了这个主题How to save My Data Type in NSUserDefault?并从那里获得了这段有用的代码:

MyObject *myObject = [[MyObject alloc] init];

NSData *myObjectData  = [NSData dataWithBytes:(void *)&myObject length:sizeof(myObject)];

[[NSUserDefaults standardUserDefaults] setObject:myObjectData forKey:@"kMyObjectData"];

用于保存数据,这用于阅读

 NSData *getData = [[NSData alloc] initWithData:[[NSUserDefaults standardUserDefaults] objectForKey:@"kMyObjectData"]];

MyObject *getObject;

[getData getBytes:&getObject];

当我将数据保存在一个ViewController中并在另一个中读取时,它的工作非常好。 但是当我想在同一个班级使用它时:

 - (IBAction)linkedInLog:(UIButton *)sender
{
    NSUserDefaults *myDefaults = [[NSUserDefaults standardUserDefaults] objectForKey:@"linkedinfo"];
    NSData *getData = [[NSData alloc] initWithData:myDefaults];
    LinkedContainer *getObject;
    [getData getBytes:&getObject];
    if (!myDefaults) {
        mLogInView = [[linkedInLoginView alloc]initWithNibName:@"linkedInLogInView" bundle:nil];
            [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(loginViewDidFinish:)
                                                     name:@"loginViewDidFinish"
                                                   object:mLogInView];
        [self.navigationController pushViewController:mLogInView animated:YES];
        if ((FBSession.activeSession.isOpen)&&(mLinkedInIsLogegOn)) {
            mMergeButton.hidden = NO;
        }
    }
    else{
        mLinkedInIsLogegOn= YES;
        mLinkedInInfo.mConsumer = getObject.mConsumer;
        mLinkedInInfo.mToken = getObject.mToken;
    }
}
出了问题。在@selector中:loginViewDidFinish我将我的数据保存到NSUserDefaults

    -(void) loginViewDidFinish:(NSNotification*)notification
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    mLinkedInInfo.mConsumer = mLogInView.consumer;
    mLinkedInInfo.mToken = mLogInView.accessToken;
    NSData *myObjectData  = [NSData dataWithBytes:(void *)&mLinkedInInfo length:sizeof(mLinkedInInfo)];
    NSUserDefaults *lSave = [NSUserDefaults standardUserDefaults];
    [lSave setObject:myObjectData forKey:@"linkedinfo"];
    [lSave synchronize];
    if (mLinkedInInfo.mToken) {
        mLinkedInIsLogegOn = YES;
    }   
}

当涉及到其他部分时,程序总是会崩溃。如果有人知道我做错了什么,请帮助我)

错误消息:编译getObject.Consumer

时,线程1:EXC_BAD_ACCESS(代码= 2,地址0x8)

2 个答案:

答案 0 :(得分:4)

在绝大多数情况下,这不是将对象序列化为NSData的有意义的方法:

MyObject *myObject = [[MyObject alloc] init];

NSData *myObjectData  = [NSData dataWithBytes:(void *)&myObject length:sizeof(myObject)];

[[NSUserDefaults standardUserDefaults] setObject:myObjectData forKey:@"kMyObjectData"];

这样做的规范方法是让MyObject采用NSCoding协议。根据您在此处发布的代码,采用NSCoding可能如下所示:

- (id)initWithCoder:(NSCoder *)coder
{
    if (self = [super init])
    {
        mConsumer = [coder decodeObjectForKey: @"consumer"];
        mToken = [coder decodeObjectForKey: @"token"];
    }
    return self;
}

- (void)encodeWithCoder:(NSCoder *)coder 
{
    [coder encodeObject:mConsumer forKey: @"consumer"];
    [coder encodeObject:mToken forKey:@"token"];
}

完成这项工作后,您可以像这样将NSObject转换为NSData和从NSData转换:

NSData* data = [NSKeyedArchiver archivedDataWithRootObject: myObject];
MyObject* myObject = (MyObject*)[NSKeyedUnarchiver unarchiveObjectWithData: data];

你在这里的代码完全会破坏堆栈并崩溃(因为这行[getData getBytes:&getObject];将导致NSData将字节写入getObject的地址,这是本地声明的堆栈。因此堆栈粉碎。)从您的代码开始,一个工作实现可能看起来像这样:

- (IBAction)linkedInLog:(UIButton *)sender
{
    NSData* dataFromDefaults = [[NSUserDefaults standardUserDefaults] objectForKey:@"linkedinfo"];
    LinkedContainer* getObject = (LinkedContainer*)[NSKeyedUnarchiver unarchiveObjectWithData: dataFromDefaults];
    if (!dataFromDefaults) {
        mLogInView = [[linkedInLoginView alloc]initWithNibName:@"linkedInLogInView" bundle:nil];
        [[NSNotificationCenter defaultCenter] addObserver:self
                                                 selector:@selector(loginViewDidFinish:)
                                                     name:@"loginViewDidFinish"
                                                   object:mLogInView];
        [self.navigationController pushViewController:mLogInView animated:YES];
        if ((FBSession.activeSession.isOpen)&&(mLinkedInIsLogegOn)) {
            mMergeButton.hidden = NO;
        }
    }
    else{
        mLinkedInIsLogegOn= YES;
        mLinkedInInfo.mConsumer = getObject.mConsumer;
        mLinkedInInfo.mToken = getObject.mToken;
    }
}

-(void) loginViewDidFinish:(NSNotification*)notification
{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
    mLinkedInInfo.mConsumer = mLogInView.consumer;
    mLinkedInInfo.mToken = mLogInView.accessToken;
    NSData* objectData = [NSKeyedArchiver archivedDataWithRootObject: mLinkedInInfo];
    [[NSUserDefaults standardUserDefaults] setObject: objectData forKey: @"linkedinfo"];
    [[NSUserDefaults standardUserDefaults] synchronize];
    if (mLinkedInInfo.mToken) {
        mLinkedInIsLogegOn = YES;
    }
}

答案 1 :(得分:3)

我同意ipmcc的回答,另一个可行的选择是向对象添加方法以将其转换为NSDictionary。您也可以向-initWithDictionary添加方法,并且应该使实例化变得非常容易。从NSUserDefaults中的字典中拉出来使用,转换为字典进行保存。

以下是具有通用数据的这两种方法的示例:

- (id)initWithDictionary:(NSDictionary *)dict
{
    self = [super init];

    // This check serves to make sure that a non-NSDictionary object
    // passed into the model class doesn't break the parsing.
    if(self && [dict isKindOfClass:[NSDictionary class]]) {
        NSObject *receivedFences = [dict objectForKey:@"fences"];
        NSMutableArray *parsedFences = [NSMutableArray array];
        if ([receivedFences isKindOfClass:[NSArray class]]) {
            for (NSDictionary *item in (NSArray *)receivedFences) {
                if ([item isKindOfClass:[NSDictionary class]]) {
                    [parsedFences addObject:[Fences modelObjectWithDictionary:item]];
                }
            }
        }
    }
    // More checks for specific objects here

    return self;

}

- (NSDictionary *)dictionaryRepresentation
{
    NSMutableDictionary *mutableDict = [NSMutableDictionary dictionary];
    NSMutableArray *tempArrayForFences = [NSMutableArray array];
    for (NSObject *subArrayObject in self.fences) {
        if([subArrayObject respondsToSelector:@selector(dictionaryRepresentation)]) {
            // This class is a model object
            [tempArrayForFences addObject:[subArrayObject performSelector:@selector(dictionaryRepresentation)]];
        } else {
            // Generic object
            [tempArrayForFences addObject:subArrayObject];
        }
    }

    [mutableDict setValue:[NSArray arrayWithArray:tempArrayForFences] forKey:@"fences"];

    return [NSDictionary dictionaryWithDictionary:mutableDict];
}

这基本上是由我使用的程序JSON Accelerator生成的样板代码。它将读取API返回的JSON字符串并为您生成对象代码。这不是一个新概念,但是为API创建的类非常容易。这段代码非常适合创建要保存到NSUserDefaults的词典。希望这会有所帮助。