2014-12-31 21 views
7

JSONModelを使用してモデルを解析しているときに、この例外が見つかりました。それは[NSObject setValue:forKey:]に依存しているためNSInvalidArgumentException - [__ NSCFString unsignedLongLongValue]:インスタンスに送信された認識できないセレクタ

NSInvalidArgumentException -[__NSCFString unsignedLongLongValue]: unrecognized selector sent to instance 0x1782210c0 

問題がJSONModel.m内の場所を得ました。

簡単に再現する方法が見つかりました。

@property NSUInteger uintegerProperty; 
[...] 
[self setValue:@"1" forKey:@"uintegerProperty"]; 

setValueNSStringで定義されたlongLongVaueを呼び出してしまうので、これは32ビットで動作しますが、64ビットの場合には、NSStringで定義されていないunsignedLongLongValueを呼び出すです。

私の特定のケースでは、問題はenumがモデルで使用されていることです。

typedef NS_ENUM(NSUInteger, kNotificationTypeEnum) 
{ 
    kNotificationTypeResponse = 1, 
    kNotificationTypeAlert = 2 
}; 

この状況を処理する最善の方法は何ですか?

スタックトレース

2014-12-31 17:48:43.789 mobile-iOS[17851:613] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString unsignedLongLongValue]: unrecognized selector sent to instance 0x10f1feeb0' 
*** First throw call stack: 
(
    0 CoreFoundation      0x0000000111b99495 __exceptionPreprocess + 165 
    1 libobjc.A.dylib      0x00000001118f099e objc_exception_throw + 43 
    2 CoreFoundation      0x0000000111c2a65d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205 
    3 CoreFoundation      0x0000000111b8ad8d ___forwarding___ + 973 
    4 CoreFoundation      0x0000000111b8a938 _CF_forwarding_prep_0 + 120 
    5 Foundation       0x000000010ff2449b _NSSetUnsignedLongLongValueForKeyWithMethod + 63 
    6 Foundation       0x000000010fed5530 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 259 
    7 mobile-iOS       0x000000010ee1834c -[AppDelegate application:didFinishLaunchingWithOptions:] + 140 
    8 UIKit        0x000000011051b3d9 -[UIApplication _handleDelegateCallbacksWithOptions:isSuspended:restoreState:] + 264 
    9 UIKit        0x000000011051bbe1 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1605 
    10 UIKit        0x000000011051fa0c -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 660 
    11 UIKit        0x0000000110530d4c -[UIApplication handleEvent:withNewEvent:] + 3189 
    12 UIKit        0x0000000110531216 -[UIApplication sendEvent:] + 79 
    13 mobile-iOS       0x000000010eedf48b -[MMApplication sendEvent:] + 331 
    14 UIKit        0x0000000110521086 _UIApplicationHandleEvent + 578 
    15 GraphicsServices     0x0000000112df671a _PurpleEventCallback + 762 
    16 GraphicsServices     0x0000000112df61e1 PurpleEventCallback + 35 
    17 CoreFoundation      0x0000000111b1b679 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 41 
    18 CoreFoundation      0x0000000111b1b44e __CFRunLoopDoSource1 + 478 
    19 CoreFoundation      0x0000000111b44903 __CFRunLoopRun + 1939 
    20 CoreFoundation      0x0000000111b43d83 CFRunLoopRunSpecific + 467 
    21 UIKit        0x000000011051f2e1 -[UIApplication _run] + 609 
    22 UIKit        0x0000000110520e33 UIApplicationMain + 1010 
    23 mobile-iOS       0x000000010ee2af23 main + 243 
    24 libdyld.dylib      0x000000011230b5c9 start + 1 
) 
libc++abi.dylib: terminating with uncaught exception of type NSException 
Signal: 6 (signal SIGABRT) 
+0

あなたは 'NSString'ではなく' NSNumber'として値を渡すことができますか?私は、 '... setValue:@ 1 ...'代わりに? –

+0

はい、それは動作しますが、私の場合、NSStringとして送信されるWebフィードから直接データを解析しています。 – 7ynk3r

+0

例外スタックトレース? –

答えて

3

最も簡単な方法は、ただのuint32_tのように、プロパティの短い整数型を使用することが考えられます。 2番目に簡単な方法は、欠落しているメソッドを提供するNSStringにカテゴリを追加することです。

+1

' - (unsigned long型ロング)unsignedLongLongValue \t {\t \t戻りself.longLongValue。 \t} 'ありがとう! – 7ynk3r

6

この問題を解決するための仕上げソリューションは:

@implementation NSString (UnsignedLongLongValue) 

- (unsigned long long)unsignedLongLongValue { 

    return self.longLongValue; 
} 

@end 

ありがとう!

0

unsignedLongLongValueの代わりにlongValueというエラーが発生したことを除いて、私たちはiPhone 5と同様のエラーが発生しました。だから私たちは次のコードで同じカテゴリのアプローチを使いました:

@implementation NSString (LongValue) 

- (long)longValue { 
    return (long) self.longLongValue; 
} 

@end 
関連する問題