Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/116.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Iphone 从NSRegularExpression匹配中获取NSRange_Iphone_Ios_Objective C_Nsrange - Fatal编程技术网

Iphone 从NSRegularExpression匹配中获取NSRange

Iphone 从NSRegularExpression匹配中获取NSRange,iphone,ios,objective-c,nsrange,Iphone,Ios,Objective C,Nsrange,我有一个包含NSRange元素的数组 现在,我可以使用[resultArray lastObject]获取数组的最后一个range元素 当我访问最后一个元素时,它返回一些未知对象。(表示未知类别) 现在我想将对象强制转换为NSRange。怎么做 我的代码是: NSError *error = NULL; NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:paraStartRegEx opt

我有一个包含
NSRange
元素的数组

现在,我可以使用
[resultArray lastObject]
获取数组的最后一个range元素

当我访问最后一个元素时,它返回一些未知对象。(表示未知类别)

现在我想将对象强制转换为
NSRange
。怎么做

我的代码是:

NSError *error = NULL;
NSRegularExpression *regex = [NSRegularExpression regularExpressionWithPattern:paraStartRegEx options:NSRegularExpressionCaseInsensitive error:&error];
NSArray *matches = [regex matchesInString:content options:0 range:NSMakeRange(0, [content length])];
NSObject *obj =  [matches lastObject];

如何将此对象转换为
NSRange

如果您阅读了文档,您会注意到
NSRegularExpression
方法返回
NSTextCheckingResult
对象的
NSArray
,而不是
NSRange
s,所以你不能直接把它们弄出来也就不足为奇了

此外(您应该记住这一点,因为这是一个重要的区别),
NSRange
是一个C
struct
,而不是Objective-C对象。这意味着它不能直接存储在
NSArray
或其他Objective-C对象容器中。
NSArray
NSDictionary
,以及用于存储对象的其他Objective-C类只能存储Objective-C类型的对象,即。,对象,这些对象是
类的实例,可以使用
[对象方法]
向其发送消息

现在回到您的问题,您从
匹配安装:选项:范围:
获取的数组包含
NSTextCheckingResult
对象,这些对象包含的信息比匹配范围多得多,因此您不能将其强制转换为
NSRange
;您需要访问对象的
范围
属性以获得所需内容:

NSTextCheckingResult *result = [matches lastObject];
NSRange range = [result range];

NSRange是一个C结构

NSArray不包含C类型,仅包含对象

NSRegularExpression返回一个匹配的NSArray,其中包含NSTextCheckingResult类型的对象


NSTextCheckingResult有一个range属性,返回匹配的范围

谢谢你分享你的知识