How to derive an integer value from NSRange?

I have NSRange and you need to split the string into two substrings on either side of this NSRange. How to get an integer value (e.g. index) from NSRange?

+6
source share
2 answers

NSRange struct consists of two integers - location and length .

 typedef struct _NSRange { NSUInteger location; NSUInteger length; } NSRange; 

It appears that location and location+length are the two expressions you are looking for - ranges for left and right substrings:

 NSRange prefixRange = NSMakeRange(0, myRange.location); NSUInteger pos =myRange.location+myRange.length; NSRange suffixRange = NSMakeRange(pos, myString.length - pos); 
+4
source

I know this question is old, but in Swift 4 (and maybe earlier) in NSRange there are new convenience upperBound and lowerBound :

 print(myRange) // Range: {13, 4} range.lowerBound // 13 range.upperBound // 17 

Hope this helps someone else.

0
source

Source: https://habr.com/ru/post/923872/


All Articles