I am creating one app in which I am saving my array of custom object in NSUserDefaults using this below code :
[Util setSubProductsArrayPreference:myArray forKey:productId];
//Util.m
+(void)setSubProductsArrayPreference:(NSMutableArray *)subProducts forKey:(NSString *)string
{
for (int i=0; i<subProducts.count; i++) {
SubProducts *po=[subProducts objectAtIndex:i];
NSString *st=[NSString stringWithFormat:@"%d",i];
[Util setSubProductsPreference:po forKey:st];
}
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSData *myEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:subProducts];
[prefs setObject:myEncodedObject forKey:string];
[prefs synchronize];
}
+(void)setSubProductsPreference:(SubProducts *)subProducts forKey:(NSString *)key
{
NSUserDefaults *prefs = [NSUserDefaults standardUserDefaults];
NSData *myEncodedObject = [NSKeyedArchiver archivedDataWithRootObject:subProducts];
[prefs setObject:myEncodedObject forKey:key];//here it is crashing
[prefs synchronize];
}
This code is working fine with iOS7 and prior..The problem is when I am running my same code with iOS8 in xCode6 then I am getting the below error :
-[__NSCFNumber length]: unrecognized selector sent to instance 0xb000000000000f23
*** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber length]: unrecognized selector sent to instance 0xb000000000000f23'
*** First throw call stack:
(
0 CoreFoundation 0x00000001042fbf35 __exceptionPreprocess + 165
1 libobjc.A.dylib 0x0000000103bbabb7 objc_exception_throw + 45
2 CoreFoundation 0x000000010430304d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x000000010425b27c ___forwarding___ + 988
4 CoreFoundation 0x000000010425ae18 _CF_forwarding_prep_0 + 120
5 CoreFoundation 0x00000001042bc790 _CFPrefsEncodeKeyValuePairIntoMessage + 64
6 CoreFoundation 0x00000001042fe286 -[CFPrefsPlistSource sendMessageSettingValue:forKey:] + 102
7 CoreFoundation 0x000000010423cef7 -[CFPrefsPlistSource alreadylocked_setValue:forKey:] + 215
8 CoreFoundation 0x000000010423cdee -[CFPrefsSource setValue:forKey:] + 62
9 CoreFoundation 0x00000001041fa1f8 +[CFPrefsSource withSourceForIdentifier:user:byHost:container:perform:] + 1112
10 CoreFoundation 0x000000010423cd63 _CFPreferencesSetValueWithContainer + 227
11 Foundation 0x000000010374099b -[NSUserDefaults(NSUserDefaults) setObject:forKey:] + 46
12 HP ProTrain 0x0000000101186787 +[Util setSubProductsArrayPreference:forKey:] + 487
13 HP ProTrain 0x0000000101115a38 __74-[DesktopProductsViewController fetchingProductSubCategoryDataFromServer:]_block_invoke_2 + 3096
14 libdispatch.dylib 0x00000001051b4ba6 _dispatch_call_block_and_release + 12
15 libdispatch.dylib 0x00000001051d27f4 _dispatch_client_callout + 8
16 libdispatch.dylib 0x00000001051bb8fb _dispatch_main_queue_callback_4CF + 949
17 CoreFoundation 0x0000000104263fe9 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
18 CoreFoundation 0x0000000104226eeb __CFRunLoopRun + 2043
19 CoreFoundation 0x0000000104226486 CFRunLoopRunSpecific + 470
20 GraphicsServices 0x00000001082c29f0 GSEventRunModal + 161
21 UIKit 0x0000000102463420 UIApplicationMain + 1282
22 HP ProTrain 0x00000001010c1a87 main + 151
23 libdyld.dylib 0x0000000105207145 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
Do any one else experience the same problem?
Change this line
[Util setSubProductsArrayPreference:myArray forKey:[NSString stringWithFormat:@"%@",productId]];