Search code examples
iosobjective-cnsstringnsdatanscoder

My Objects data member (NSString *) value contaminated '0x7fc0ab73d040' after UnArchiving from NSData


I am in such a hectic situation right now for almost a whole day. Please see and tell if you can help...

Here I am using my CustomObjects ('Alarm Object' implemented with NSCoder), saving it to NSUserDefaults

AlarmObject has one property (NSString *) 'tuneAddress' having value like

"tunes/Alarm%20clock%20ringtones%20-%20free%20download.%20Mp3%20Alarm%20clock%20tones,%20sounds%20and%20ringtones%20for%20mobile%20phones.mp3"

I know its not good to have spaces resolved in the address like this (%20) but somehow I am not able to change this. It's coming from server that is not yet in my control.

Well, First Issue issue here is whenever I add some object to NSUserDefaults using "NSKeyedArchiver archivedDataWithRootObject" with proper such value like shown above to 'tuneAddress' and when a try to have it back through "NSKeyedUnarchiver unarchiveObjectWithData".

Here I get the objects back with 'tuneAddress' value as '0x7fc0ab73d040'. Please look on it If you can say something about what is this..?

Second issue is dependent on first one when I try archive it back through "NSKeyedArchiver archivedDataWithRootObject" it stops here

[encoder encodeObject:self.tuneLocalAddress forKey:@"tuneLocalAddress"];

revealing error EXC_BAD_ACCESS(code=EXC_i386_GPFLT).

Here is a little show how I do save the pointed string to AlarmObject

[[NSUserDefaults standardUserDefaults] setObject:[newString stringByReplacingOccurrencesOfString:@" " withString:@"%20"] forKey:TuneChoosenForAlarm_Key];

AlarmObject * newAlarmObj = [[AlarmObject alloc] init];
newAlarmObj.tuneLocalAddress = [[NSUserDefaults standardUserDefaults] objectForKey:TuneChoosenForAlarm_Key];

NSMutableArray * newArray = [[NSMutableArray alloc] initWithObjects:newAlarmObj, nil];

for (AlarmObject * alarmObj in self.alarmsArray) {
        [newArray addObject:alarmObj];
}
self.alarmsArray = newArray;

NSData *data = [NSKeyedArchiver archivedDataWithRootObject:self.alarmsArray];
[[NSUserDefaults standardUserDefaults] setObject:data forKey:AlarmsDataDictonaryKey];

Solution

  • Here, I found the reason, mistake was on base level. While creating 'AlarmObject' here was 'tuneLocalAddress' with memory property "assign" this exactly where I was wrong.

    If you are working with ARC and want to retain the value of some property (tuneLocalAddress) in the memory until that object is in use, you should use 'retain' or 'strong' having a little idea from here.

    I hope you can understand. Sorry, for any grammar mistakes.