'*** -[__NSCFString dataUsingEncoding:allowLossyConversion:]: didn't convert all characters'

Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '*** -[__NSCFString dataUsingEncoding:allowLossyConversion:]: didn't convert all characters

我在用下面方法计算长度时,出现的bug:

- (CGFloat)lp_length

{

    NSStringEncoding enc = CFStringConvertEncodingToNSStringEncoding(kCFStringEncodingGB_18030_2000);

    NSData *data = [self dataUsingEncoding:enc];

    NSUInteger contentLength = [data length]/2;

    return contentLength;

}

当我用字符串直接计算时[string lp_length], 会崩。具体原因不清楚, 猜测是系统bug。

解决方案:

我用textView.text = string;

然后用[textView.text lp_length]去计算就没事了, 神不神奇

你可能感兴趣的:('*** -[__NSCFString dataUsingEncoding:allowLossyConversion:]: didn't convert all characters')