While running this code:
NSData *archivedSavedData = [[NSData alloc] init];
archivedSavedData = [defaults objectForKey:@"listOfAccessNumbers"];
NSLog(@"archivedSavedData length is %d", [archivedSavedData length] );
I am getting this crash error (last line) only when running on a device that is connected:
[__NSCFArray length]: unrecognized selector sent to instance 0x2398a0
2012-03-13 20:25:33.088[7301:707] * Terminating app due to uncaught exception ‘NSInvalidArgumentException’, reason: ‘-[__NSCFArray length]: unrecognized selector sent to instance 0x2398a0’
* First throw call stack:
(0x34dbc88f 0x361e3259 0x34dbfa9b 0x34dbe915 0x34d19650 0xccb1b 0x31e13e33 0x31e38629 0x31dfcd7d 0x31ebf4dd 0x31e0555d 0x31e05579 0x31e0540b 0x31e053e7 0xcfedf 0x31e12e53 0x31e0c985 0x31ddac6b 0x31dda70f 0x31dda0e3 0x3600f22b 0x34d90523 0x34d904c5 0x34d8f313 0x34d124a5 0x34d1236d 0x31e0ba13 0x31e08e7d 0xcfd39 0xcbe28)
terminate called throwing an exception
This doesn’t happen when running on the simulator or directly on the device with a distribution profile (through testflight for example).
Does anyone know how such a behavior could happen only in this case?
Thanks.
UPDATE: when trying to replace length with count I get this complication error: “No visible @interface for ‘NSData’ declares the selector ‘count'”
UPDATE2: I understand that it should be an NSArray rather than an NSData, but my problem is that I did store archived NSData cause my array consists of custom objects, so I had to archived this data into NSData format when saving in NSUserDefault. How else should I approach that otherwise?
Thats how I store the data:
NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults];
NSData *data = [NSKeyedArchiver archivedDataWithRootObject:array];
[defaults setObject:data forKey:@"listOfAccessNumbers"];
array is an array of custom objects of the form of:
@interface NumberDataObj : NSObject {
NSString *inputName;
NSString *inputNum;
}
The error message says:
That means that
archivedSavedDatais an array and that it doesn’t (obviously) respond tolengthso you should declarearchivedSavedDataas an array and usecountinstead.Now, as to why this doesn’t happen when running on the simulator, my guess is that your test scenarios don’t make this part of the code get called.
EDIT
If you want to retrieve the data as
NSDatathen use the methoddataForKey:The documentation says for
dataForKey:and for
arrayForkey:So aways use the appropriate method when you know the type of the data to avoid problems like this.