wayneh
wayneh

Reputation: 4413

NSCoding - reuse object for writing?

I have a Singleton which has an NSMutableArray containing a Class, the Class contains the proper NSCoding routines to encode/decode the data - it all works fine.

However I'd now like to also save data that is not part of the Class (array), but instead is part of the Singleton and is not specific to each item in the Class/array. So I've added the appropriate code in the Singleton including:

    BOOL            alarmIsOn;
    ...
    @property(nonatomic,assign)   BOOL            alarmIsOn;
    ...
    @synthesize alarmIsOn;
    ...
    [encoder encodeBool:alarmIsOn forKey:@"alarmison"];
    ...
    alarmIsOn=[decoder decodeBoolForKey:@"alarmison"];

When I save my data I previously used this which works perfectly:

GlobalData *globDat=[GlobalData getSingleton];
NSData *encodedObject = [NSKeyedArchiver archivedDataWithRootObject:globDat.allMsgs];
[encodedObject writeToFile:plistPath atomically:YES];

Now I'd like to add the following to include the additional data from the Singleton:

encodedObject = [NSKeyedArchiver archivedDataWithRootObject:globDat.alarmIsOn];
[encodedObject writeToFile:plistPath atomically:YES];

However it gives me this error in Xcode:

 Automatic Reference Counting Issue: Implicit conversion of 'BOOL' (aka 'signed char') to 'id' is disallowed with ARC

And this warning:

Semantic Issue: Incompatible integer to pointer conversion sending 'BOOL' (aka 'signed char') to parameter of type 'id'

What am I doing wrong, and how can I fix this?

Upvotes: 1

Views: 324

Answers (1)

DRVic
DRVic

Reputation: 2481

Your error is that globDat.alarmIsOn is a bool and

NSKeyedArchiver archivedDataWithRootObject:

wants an

id

which is another word for an opaque pointer to an object. A bool is just a byte. How exactly you want to fix it is up to you. To use that routine requires an object.

Upvotes: 3

Related Questions