Question

Can someone explain what location and length represent for NSRange. If I use it in this context

NSRange range = [self.display.text rangeOfString:@"."];
if(range.location == NSNotFound){
self.display.text = [self.display.text stringByAppendingString:@"."];

What does the location represent and can someone explain this code. Also where can I find more information on properties such as location> I found it in the header file as an NSUInteger but it doesn't describe what location actually does.

Was it helpful?

Solution

According to the official doc:

rangeofString is used to finds and returns the range of the first occurrence of a given string within the receiver.

The index of the first occurrence within your original string will be stored in the location attribute of NSRange. In case there is no occurence found the method will return NSNotFound.

So your code will append the string your are testing with '.' if this character was not found in it.

OTHER TIPS

The location field is the location, or index, of the NSRange - in your case it is the index of the string "." within the original string. The length field is the length of the range that the NSRange implements.

You can also find the definition in the reference docs.

Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top