项目中出现在UITextField输入时程序崩溃,崩溃提示信息如下:
<Error>: CGContextSetFillColorWithColor: invalid context 0x0. Backtrace:
<<redacted>+72>
<<redacted>+328>
<<redacted>+28>
<<redacted>+1032>
<<redacted>+6760>
<<redacted>+520>
<<redacted>+532>
<<redacted>+1120>
<<redacted>+2636>
<<redacted>+1088>
<<redacted>+80>
<<redacted>+600>
<<redacted>+148>
<<redacted>+60>
<<redacted>+64>
<<redacted>+148>
<<redacted>+392>
<<redacted>+1060>
<<redacted>+3720>
<<redacted>+676>
<<redacted>+64>
<<redacted>+188>
<<redacted>+1196>
<<redacted>+148>
<<redacted>+292>
<<redacted>+32>
<<redacted>+252>
<<redacted>+512>
<<redacted>+324>
<<redacted>+32>
<<redacted>+372>
<<redacted>+1024>
<CFRunLoopRunSpecific+444>
<GSEventRunModal+180>
<<redacted>+684>
<UIApplicationMain+208>
<main+124>
1480921050.774281 hight_hitht:258.000000
libc++abi.dylib: terminate_handler unexpectedly threw an exception
这问题在模拟器上是没有的,在真机上有时能重现,有时又正常,相当诡异,找了大半天还是没有找到解决办法,后来第二天早上突然被我找到是哪里的问题了。起初怀疑是xcode8.1的问题,因为这代码一直工作得好好的,好像就是最近换了xcode8.1才出现这样的问题。 后来证实我是我错怪了xcode8.1,虽然网上的确有不少人吐槽xcode8.1的各种bug。在英文和拼音输入法下输入是不会崩溃的,只是在中文手写输入法会崩溃; 原来是跟工程中的一个UIScrollView的category有关,把下面的代码去掉就正常,具体原来我也不是很清楚,应该是手写输入法跟这个UIScrollView是有关系的。
最后不得不感叹下,有的问题说不上是一个重要的知识点,但是一旦遇到还是会卡上你不少时间。
import "UIScrollView+Touch.h"
@implementation UIScrollView (Touch)
// 下面的代码有bug,会导致UITextField在使用手写中文输入的时候崩溃
(void)touchesBegan:(NSSet *)touches withEvent:(UIEvent *)event {
[[self nextResponder] touchesBegan:touches withEvent:event];
[super touchesBegan:touches withEvent:event];
}
-(void)touchesMoved:(NSSet *)touches withEvent:(UIEvent *)event {
[[self nextResponder] touchesMoved:touches withEvent:event];
[super touchesMoved:touches withEvent:event];
}(void)touchesEnded:(NSSet *)touches withEvent:(UIEvent *)event {
[[self nextResponder] touchesEnded:touches withEvent:event];
[super touchesEnded:touches withEvent:event];
}
@end