[__NSCFString charValue]:无法识别的选择器发送到实例?

3

我的应用程序使用FMDB和Mantle,iPhone 5模拟器之后一切正常,但在iPhone 5s之前崩溃了,看起来布尔变量是原因。但我不明白为什么iPhone 5s、6和6 Plus上一切都正常。

@property (nonatomic, assign) BOOL isMyFan;
@property (nonatomic, assign) BOOL isMyFollow;
@property (nonatomic, assign) NSInteger cityID;
@property (nonatomic, assign) NSInteger provinceID;
@property (nonatomic, assign) NSInteger locationID;

2015-07-14 21:33:17.264 ATOMPSGod[48894:607] -[__NSCFString charValue]: unrecognized selector sent to instance 0x79fb7700
2015-07-14 21:33:17.268 ATOMPSGod[48894:607] *** Caught exception setting key "isMyFollow" : -[__NSCFString charValue]: unrecognized selector sent to instance 0x79fb7700
2015-07-14 21:33:17.277 ATOMPSGod[48894:607] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFString charValue]: unrecognized selector sent to instance 0x79fb7700'
*** First throw call stack:
(
    0   CoreFoundation                      0x026201e4 __exceptionPreprocess + 180
    1   libobjc.A.dylib                     0x022ba8e5 objc_exception_throw + 44
    2   CoreFoundation                      0x026bd243 -[NSObject(NSObject) doesNotRecognizeSelector:] + 275
    3   CoreFoundation                      0x0261050b ___forwarding___ + 1019
    4   CoreFoundation                      0x026100ee _CF_forwarding_prep_0 + 14
    5   Foundation                          0x01f3eb39 _NSSetCharValueForKeyWithMethod + 67
    6   Foundation                          0x01ee727f _NSSetUsingKeyValueSetter + 256
    7   Foundation                          0x01ee6731 -[NSObject(NSKeyValueCoding) setValue:forKey:] + 267
    8   ATOMPSGod                           0x0020f1a7 MTLValidateAndSetValue + 311
    9   ATOMPSGod                           0x0020ef19 -[MTLModel initWithDictionary:error:] + 697
    10  ATOMPSGod                           0x0020eb9e +[MTLModel modelWithDictionary:error:] + 126
    11  ATOMPSGod                           0x000e3d54 -[MTLFMDBAdapter initWithFMResultSet:modelClass:error:] + 6836
    12  ATOMPSGod                           0x000e20fb +[MTLFMDBAdapter modelOfClass:fromFMResultSet:error:] + 139
    13  ATOMPSGod                           0x00139746 __25+[ATOMUserDAO fetchUser:]_block_invoke + 246
    14  ATOMPSGod                           0x00299cec __30-[FMDatabaseQueue inDatabase:]_block_invoke + 108
    15  libdispatch.dylib                   0x03c124d0 _dispatch_client_callout + 14
    16  libdispatch.dylib                   0x03bff740 _dispatch_barrier_sync_f_invoke + 58
    17  libdispatch.dylib                   0x03bff3ea dispatch_barrier_sync_f + 89
    18  libdispatch.dylib                   0x03bff90b dispatch_sync + 37
    19  ATOMPSGod                           0x00299c15 -[FMDatabaseQueue inDatabase:] + 341
    20  ATOMPSGod                           0x0013954e +[ATOMUserDAO fetchUser:] + 286
    21  ATOMPSGod                           0x00133c9e -[ATOMCurrentUser fetchCurrentUserInDB:] + 190
    22  ATOMPSGod                           0x0002e9f7 -[AppDelegate application:didFinishLaunchingWithOptions:] + 663
    23  UIKit                               0x02c3314f -[UIApplication _handleDelegateCallbacksWithOptions:isSuspended:restoreState:] + 309
    24  UIKit                               0x02c33aa1 -[UIApplication _callInitializationDelegatesForURL:payload:suspended:] + 1810
    25  UIKit                               0x02c38667 -[UIApplication _runWithURL:payload:launchOrientation:statusBarStyle:statusBarHidden:] + 824
    26  UIKit                               0x02c4cf92 -[UIApplication handleEvent:withNewEvent:] + 3517
    27  UIKit                               0x02c4d555 -[UIApplication sendEvent:] + 85
    28  UIKit                               0x02c3a250 _UIApplicationHandleEvent + 683
    29  GraphicsServices                    0x050cff02 _PurpleEventCallback + 776
    30  GraphicsServices                    0x050cfa0d PurpleEventCallback + 46
    31  CoreFoundation                      0x0259bca5 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE1_PERFORM_FUNCTION__ + 53
    32  CoreFoundation                      0x0259b9db __CFRunLoopDoSource1 + 523
    33  CoreFoundation                      0x025c668c __CFRunLoopRun + 2156
    34  CoreFoundation                      0x025c59d3 CFRunLoopRunSpecific + 467
    35  CoreFoundation                      0x025c57eb CFRunLoopRunInMode + 123
    36  UIKit                               0x02c37d9c -[UIApplication _run] + 840
    37  UIKit                               0x02c39f9b UIApplicationMain + 1225
    38  ATOMPSGod                           0x00112cba main + 138
    39  libdyld.dylib                       0x03e466d9 start + 1
)

libc++abi.dylib: 发生了未捕获的 NSException 异常而终止 (lldb)

更新后的变量视图:

key __NSCFString *  @"isMyFollow"   0x79e50400
obj ATOMUser *  0x79e46e30  0x79e46e30
ATOMBaseModel   ATOMBaseModel       
_boundWeibo BOOL    NO  '\0'
_boundWechat    BOOL    NO  '\0'
_isMyFan    BOOL    NO  '\0'
_isMyFollow BOOL    NO  '\0'
_uid    int 0   0
_cityID NSInteger   0   0
_provinceID NSInteger   0   0
_locationID NSInteger   0   0
_mobile NSString *  nil 0x00000000
_nickname   NSString *  nil 0x00000000
_avatar NSString *  nil 0x00000000
_backgroundImage    NSString *  nil 0x00000000
_sex    NSInteger   0   0
_attentionNumber    NSInteger   11  11
_fansNumber NSInteger   0   0
_praiseNumber   NSInteger   0   0
_uploadNumber   NSInteger   0   0
_replyNumber    NSInteger   0   0
_proceedingNumber   NSInteger   0   0
_attentionUploadNumber  NSInteger   0   0
_attentionWorkNumber    NSInteger   0   0
forceUpdate BOOL    YES '\x01'
value   __NSCFString *  @"0"    0x79e3e140
error   NSError **  NULL    0x00000000
validatedValue  __NSCFString *  @"0"    0x79e3e140

1
你能发布相关代码吗,特别是来自MTLValidateAndSetValue类的代码? - Glorfindel
@luk2302 不是的,isMyFollow 是用作布尔值的。 - PeiweiChen
@Glorfindel,发布了变量视图。 - PeiweiChen
1
这明显是一个未识别的选择器错误。这不可能与BOOL类型有关,正如你怀疑的和@NKorotkov在下面回答的那样。我觉得你可能遇到了一个间歇性崩溃,在32位和64位下都可能发生,只是碰巧自从将所有的BOOL改为bool(不要这样做...)之后,也无法再现它了。发布一些代码,或许你会得到一个合理的答案。 - Stephen Groom
从堆栈跟踪来看,我会从-[AppDelegate application:didFinishLaunchingWithOptions:]开始。 - Stephen Groom
显示剩余6条评论
1个回答

8
如果它在iPhone 5S及更高版本上运行,则似乎是32/64位问题,因为自5S以来的所有iPhone都是64位。更准确地说,在32位系统下,BOOLsigned char,而在64位系统下,它是bool
您可以在这里阅读更多信息。
我假设您的问题与在集合中存储BOOL值有关。当在obj-c集合中存储BOOL值时,它们被包装在NSNumber对象中,因为集合只能容纳对象。您应该像这样使用它:
NSDictionary *dict = @{@"BOOL_Key": [NSNumber numberWithBool:YES]};
BOOL myBool = [[dict objectForKey:@"BOOL_Key"] boolValue];

或者你可以使用@(YES)代替[NSNumber numberWithBool:]。如果不使用boolValue,可能会得到不可预测的结果。希望能帮到你。


非常感谢!这个问题让我浪费了一整天的时间,而你的建议让我在晚上得以解决。将所有的BOOL变量替换为bool后,我的应用程序就像魔法般地运行起来了! - PeiweiChen
@PeiweiChen 很高兴我能帮到你。但是我应该指出的是BOOL应该在32位和64位系统上正常工作。如果它不能的话,你可能在布尔类型方面做错了什么。 - NKorotkov
也许Mantle出了问题,因为它只支持iOS 8.0以上的版本。 - PeiweiChen
你至少已经有两年没有必要使用numberWithBool:了,@(someBool)足以将bool/BOOL封装成NSNumber。 - Steve Wilford
@SteveWilford 是的,但我想更明确地说明值是如何处理的。 - NKorotkov

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接