[__NSCFNumber 长度]:无法识别的选择器发送到具有 NSUserDefaults 的实例

[__NSCFNumber length]: unrecognized selector sent to instance with NSUserDefaults

我正在创建一个应用程序,在其中使用以下代码将我的自定义对象数组保存在 NSUserDefaults 中:

[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];
}

此代码在 iOS7 和之前工作正常。问题是当我 运行 与 xCode6 中的 iOS8 相同的代码时,我得到以下错误:

-[__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

有没有其他人遇到同样的问题?

很简单,您的 productId 没有返回字符串。只需在 setObject.

之前将 productId 类型转换为 NSString

更改此行

 [Util setSubProductsArrayPreference:myArray forKey:[NSString stringWithFormat:@"%@",productId]];