错误处理:unrecognized selector sent to instance

异常:unrecognized selector sent to instance的定位

2015-08-14 15:16:10.987 MeSmart[26812:1861799] -[MeSmart.SmartLightViewController switchAction:]: unrecognized selector sent to instance 0x7faea2ceb0e0
2015-08-14 15:16:10.998 MeSmart[26812:1861799] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[MeSmart.SmartLightViewController switchAction:]: unrecognized selector sent to instance 0x7faea2ceb0e0'
*** First throw call stack:
(
0 CoreFoundation 0x000000010fbe1c65 exceptionPreprocess + 165
1 libobjc.A.dylib 0x000000010f3a1bb7 objc_exception_throw + 45
2 CoreFoundation 0x000000010fbe90ad -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
3 CoreFoundation 0x000000010fb3f13c ___forwarding
_ + 988
4 CoreFoundation 0x000000010fb3ecd8 CF_forwarding_prep_0 + 120
5 UIKit 0x000000011062d656 _UIGestureRecognizerSendActions + 262
6 UIKit 0x000000011062c2f9 -[UIGestureRecognizer _updateGestureWithEvent:buttonEvent:] + 532
7 UIKit 0x0000000110630f16 _
UIGestureRecognizerUpdate_block_invoke662 + 51
8 UIKit 0x0000000110630e12 _UIGestureRecognizerRemoveObjectsFromArrayAndApplyBlocks + 254
9 UIKit 0x0000000110626e8d _UIGestureRecognizerUpdate + 2796
10 UIKit 0x00000001102ca646 -[UIWindow _sendGesturesForEvent:] + 1041
11 UIKit 0x00000001102cb272 -[UIWindow sendEvent:] + 666
12 UIKit 0x0000000110291541 -[UIApplication sendEvent:] + 246
13 UIKit 0x000000011029ecdc _UIApplicationHandleEventFromQueueEvent + 18265
14 UIKit 0x000000011027959c _UIApplicationHandleEventQueue + 2066
15 CoreFoundation 0x000000010fb15431 __CFRUNLOOP_IS_CALLING_OUT_TO_A_SOURCE0_PERFORM_FUNCTION
_ + 17
16 CoreFoundation 0x000000010fb0b2fd __CFRunLoopDoSources0 + 269
17 CoreFoundation 0x000000010fb0a934 __CFRunLoopRun + 868
18 CoreFoundation 0x000000010fb0a366 CFRunLoopRunSpecific + 470
19 GraphicsServices 0x0000000113003a3e GSEventRunModal + 161
20 UIKit 0x000000011027c8c0 UIApplicationMain + 1282
21 MeSmart 0x000000010eb341d7 main + 135
22 libdyld.dylib 0x0000000111a62145 start + 1
23 ??? 0x0000000000000001 0x0 + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException

原因:

unrecognized selector sent to instance
给实体对象发送了不认识的消息,即对象调用方法出错(方法不存在或对象已被release)

       // 为图片增加点击动作
        var deviceImageTapGesture = UITapGestureRecognizer()
        deviceImageTapGesture.addTarget(self, action: "switchAction:")
        self.deviceImageView.userInteractionEnabled = true
        self.deviceImageView.addGestureRecognizer(deviceImageTapGesture)
  func switchAction(sender: UITapGestureRecognizer) {
        ...       
    }

这里 switchAction 后的":"表示是有参数的,所以当定义这个方法的时候就要对应的也要有入参.或者不写":",对应没有入参. 当这两个不对应的时候, 就会抛这个异常.

但其实,今天抛异常的原因是因为在方法定义的时候,把方法名写错了,把其中的两个字母顺序写反了...

参考资料
http://bugly.qq.com/blog/?p=43

2015-08-14 15:3219