Objective c NSTextCheckingResult范围返回“0”;id";而不是",;NSRange“;
我举例说明了如何在Objective-c中使用RegularExpression。 问题是我想对Linux(gnustep)和OSX使用相同的代码。 经过一些小的改动后,它可以在OSX中工作,但不能在Linux下工作Objective c NSTextCheckingResult范围返回“0”;id";而不是",;NSRange“;,objective-c,gnustep,Objective C,Gnustep,我举例说明了如何在Objective-c中使用RegularExpression。 问题是我想对Linux(gnustep)和OSX使用相同的代码。 经过一些小的改动后,它可以在OSX中工作,但不能在Linux下工作 NSArray* matches = [regex matchesInString:line options:0 range: NSMakeRange(0,[line length])]; if ([matches count]
NSArray* matches = [regex matchesInString:line options:0
range: NSMakeRange(0,[line length])];
if ([matches count] > 0) {
for (NSTextCheckingResult* match in matches) {
NSString* matchText = [line substringWithRange:[match range]];
NSLog(@"match: %@", matchText);
NSRange group1 = [match rangeAtIndex:1];
NSLog(@"group1: %@", [line substringWithRange:group1]);
}
}else{
NSLog(@"nomatch %@",[matches count]);
}
当我想编译这部分代码时,编译器将返回错误:
错误:
sending 'id' to parameter of incompatible type 'NSRange' (aka 'struct _NSRange')
NSString* matchText = [line substringWithRange:[match range]];
initializing 'NSRange' (aka 'struct _NSRange') with an expression of
incompatible type 'id'
NSRange group1 = [match rangeAtIndex:1];
我不明白为什么:-(
属性range
和rangeAtIndex
应该由NSTextCheckingResult
作为NSRange
而不是id
返回
请问有没有办法将id转换为NSRange
谢谢你我怀疑这里的问题一定是编译器找不到你的
范围
和范围索引:
方法的声明。由于找不到它们,它会将它们的返回类型默认为id
。确保你已经将带有这些方法声明的头文件导入到你引用代码所在的文件中。或者您已经导入了该头文件,但它不包含声明,因此请确保您提供的声明是绝对正确的!!!我将#导入到中,所有内容都如我所期望的那样工作。非常感谢。