Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Working with NSUserDefaults when saving own datatype

i had read this topic How to save My Data Type in NSUserDefault? and get from there this useful part of code:

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

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

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

for saving data and this for reading

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

MyObject *getObject;

[getData getBytes:&getObject];

its works very good when i save data in one ViewController and read it in other. but when i whant to use it in the same class:

 - (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;
    }
}

something going wrong. in @selector:loginViewDidFinish i am saving my data to 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;
    }   
}

the program always crashes when it comes to else part. If somebody knows what I am doing wrong please help me)

error message: Thread 1 : EXC_BAD_ACCESS(code=2,address 0x8) when compiling getObject.Consumer

like image 284
xaoc1993 Avatar asked Mar 23 '23 14:03

xaoc1993


1 Answers

In the vast majority of cases, this is not going to be a meaningful way to serialize your object into an NSData:

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

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

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

The canonical way to do this would be for MyObject to adopt the NSCoding protocol. Based on the code you posted here, an adoption of NSCoding might look like this:

- (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"];
}

Once you had done that work, you would convert MyObject to and from NSData like this:

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

The code you have here is totally going to smash the stack and crash (because this line [getData getBytes:&getObject]; will cause the NSData to write bytes to the address of getObject, which is locally declared on the stack. Hence stack smashing.) Starting from your code, a working implementation might look something like this:

- (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;
    }
}
like image 65
ipmcc Avatar answered Apr 26 '23 05:04

ipmcc