If you had read the documentation, you would have noticed that the matchesInString:options:range:
method of NSRegularExpression
returns an NSArray
of NSTextCheckingResult
objects, not NSRange
s, so it's no wonder that you couldn't get them out directly.
Additionally (and you should remember this for the future, as it is an important distinction), NSRange
is a C struct
, not an Objective-C object. This means that it cannot be directly stored in NSArray
s or other Objective-C object containers. NSArray
, NSDictionary
, and other Objective-C classes that are used for storing objects can only store objects of Objective-C types, i.e., objects that are an instance of a Class
and can have messages sent to them with [object method]
.
Now to get back to your question, the array you are getting from matchesInString:options:range:
contains NSTextCheckingResult
objects, which contain a lot more information than just the range of the match, so you can't just cast them to an NSRange
; you need to access the range
property of the object to get what you want:
NSTextCheckingResult *result = [matches lastObject];
NSRange range = [result range];