致命异常:NSInvalidArgumentException - [__ NSPlaceholderDictionary initWithObjects:forKeys:count:]:尝试从对象[1]插入nil对象

时间:2016-07-14 07:22:02

标签: objective-c nsstring nsattributedstring nsrange

当用户输入我正在更改某些文字的颜色时,我有一个UITextField。它工作正常但我在这个方法中得到了一个崩溃报告。

以下是崩溃报告:

Fatal Exception: NSInvalidArgumentException
*** -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]: attempt to insert nil object from objects[1]
Fatal Exception: NSInvalidArgumentException
0  CoreFoundation                 0x180d92db0 __exceptionPreprocess
1  libobjc.A.dylib                0x1803f7f80 objc_exception_throw
2  CoreFoundation                 0x180c7b77c -[__NSPlaceholderDictionary initWithObjects:forKeys:count:]
3  CoreFoundation                 0x180c7b614 +[NSDictionary dictionaryWithObjects:forKeys:count:]

以下是我的参考代码

-(NSMutableAttributedString *)suggestionAtributeString:(NSString *)suggestionString
{
if ([suggestionString length] == 0) {
    return nil;
}

NSString *searchString = stringByTrimmingCharactersInSet(_suggestionString, [NSCharacterSet whitespaceAndNewlineCharacterSet]);

NSMutableAttributedString * attributedString = [[NSMutableAttributedString alloc] initWithString:suggestionString];

NSRange suggestionRange2 = NSMakeRange(0, [suggestionString length]);

[attributedString setAttributes:@{
                                  NSForegroundColorAttributeName:suggestionColor2,
                                  NSFontAttributeName:_suggestionFont2
                                  }
                          range:suggestionRange2];

NSRange suggestionRange1 = [suggestionString rangeOfString:searchString options:NSCaseInsensitiveSearch];

[attributedString setAttributes:@{
                                  NSForegroundColorAttributeName:suggestionColor1,
                                  NSFontAttributeName:_suggestionFont1
                                  }
                          range:suggestionRange1];

   return attributedString;
}

0 个答案:

没有答案