Search code examples
iosnsrange

Is it possible to do negative loc in the NSMakeRange?


Is it possible to do negative loc in the NSMakeRange?

NSString *string = @"abc";
NSString *myString = [string substringWithRange:NSMakeRange(-1, 1)];

Solution

  • No.

    location and length are unsigned integers :

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

    and also, NSMakeRange function is defined as follow :

    NSRange NSMakeRange (
        NSUInteger loc,
        NSUInteger len
    );