使用NSRange的问题:索引超出范围 [英] Problem using NSRange: Index out of bounds
问题描述
我试图检测我的字符串中的网站网址,然后做一些属性字符串的东西,如粗体等。
我的正则表达式定义为:
static NSRegularExpression * websiteRegularExpression;
static inline NSRegularExpression * WebsiteRegularExpression(){
if(!websiteRegularExpression){
websiteRegularExpression = [[NSRegularExpression alloc] initWithPattern:@\b(https?| ftp | file):/ / [ - A-Z0-9 +& @#/%?=〜_ |!:,...] * [A-Z0-9 +& @#/%=〜_ |]
options:NSRegularExpressionCaseInsensitive
error:nil];
}
return websiteRegularExpression;
}
这里是我枚举的地方:
- (void)setBodyText
{
__block NSRegularExpression * regexp = nil;
bodyLabel.delegate = self;
[self.bodyLabel setText:@http://www.google.comafterInheritingLabelAttributesAndConfiguringWithBlock:^ NSAttributedString *(NSMutableAttributedString * mutableAttributedString){
NSRange stringRange = NSMakeRange(0,[mutableAttributedString长度]);
regexp = WebsiteRegularExpression();
NSRange nameRange = [regexp rangeOfFirstMatchInString:[mutableAttributedString string] options:0 range:stringRange];
UIFont * boldSystemFont = [UIFont boldSystemFontOfSize:18.0];
CTFontRef boldFont = CTFontCreateWithName((CFStringRef)boldSystemFont.fontName,boldSystemFont.pointSize,NULL);
if(boldFont){
[mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(id)boldFont range:nameRange];
CFRelease(boldFont);
}
[mutableAttributedString replaceCharactersInRange:nameRange withString:[[[mutableAttributedString string] substringWithRange:nameRange] uppercaseString]];
return mutableAttributedString;
}];
regexp = WebsiteRegularExpression();
NSRange linkRange = [regexp rangeOfFirstMatchInString:self.bodyLabel.text options:0 range:NSMakeRange(0,[bodyLabel.text length])];
[self.bodyLabel addLinkToURL:nil withRange:linkRange];
}
我得到错误:
***由于未捕获异常NSRangeException而终止应用程序,原因:'*** - [NSCFString substringWithRange:]:范围或索引超出范围
解决方案 div> 在调用 substringWithRange 之前检查 nameRange 的内容。如果您的正则表达式不匹配,则返回 rangeOfFirstMatchInString
{NSNotFound,0}
NSNotFound 定义为 NSIntegerMax ,因此您可以想象为什么此值可能超出范围
$ b >所以要检查 substringWithRange 的结果:
if(nameRange.location == NSNotFound)
//不匹配WebsiteRegularExpression()做别的事
I am trying to detect website URL's in my string and then doing some attribution string stuff like bolding it etc.
My regex is defined as:
static NSRegularExpression *websiteRegularExpression;
static inline NSRegularExpression * WebsiteRegularExpression() {
if (!websiteRegularExpression) {
websiteRegularExpression = [[NSRegularExpression alloc] initWithPattern:@"\b(https?|ftp|file)://[-A-Z0-9+&@#/%?=~_|!:,.;]*[A-Z0-9+&@#/%=~_|]"
options:NSRegularExpressionCaseInsensitive
error:nil];
}
return websiteRegularExpression;
}
Here is where I enumerate:
-(void)setBodyText
{
__block NSRegularExpression *regexp = nil;
bodyLabel.delegate = self;
[self.bodyLabel setText:@"http://www.google.com" afterInheritingLabelAttributesAndConfiguringWithBlock:^NSAttributedString *(NSMutableAttributedString *mutableAttributedString) {
NSRange stringRange = NSMakeRange(0, [mutableAttributedString length]);
regexp = WebsiteRegularExpression ();
NSRange nameRange = [regexp rangeOfFirstMatchInString:[mutableAttributedString string] options:0 range:stringRange];
UIFont *boldSystemFont = [UIFont boldSystemFontOfSize:18.0];
CTFontRef boldFont = CTFontCreateWithName((CFStringRef)boldSystemFont.fontName, boldSystemFont.pointSize, NULL);
if (boldFont) {
[mutableAttributedString addAttribute:(NSString *)kCTFontAttributeName value:(id)boldFont range:nameRange];
CFRelease(boldFont);
}
[mutableAttributedString replaceCharactersInRange:nameRange withString:[[[mutableAttributedString string] substringWithRange:nameRange] uppercaseString]];
return mutableAttributedString;
}];
regexp = WebsiteRegularExpression();
NSRange linkRange = [regexp rangeOfFirstMatchInString:self.bodyLabel.text options:0 range:NSMakeRange(0, [bodyLabel.text length])];
[self.bodyLabel addLinkToURL:nil withRange:linkRange];
}
I get the error:
*** Terminating app due to uncaught exception 'NSRangeException', reason: '*** -[NSCFString substringWithRange:]: Range or index out of bounds'
How can I resolve?
解决方案 Check the contents of nameRange before you call substringWithRange. If your regular expression doesn't match then the return value for rangeOfFirstMatchInString will be
{NSNotFound, 0}
NSNotFound is defined as NSIntegerMax so you can imagine why this value might be out of range for your mutableAttributedString.
Update for comment
So to check the results of substringWithRange:
if (nameRange.location == NSNotFound)
// didn't match the WebsiteRegularExpression() do something else
这篇关于使用NSRange的问题:索引超出范围的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!
查看全文