3

Is it possible to do negative loc in the NSMakeRange?

NSString *string = @"abc";
NSString *myString = [string substringWithRange:NSMakeRange(-1, 1)];
Voloda2
  • 12,359
  • 18
  • 80
  • 130

2 Answers2

10

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
);
0

Yes, it possible, but in fact it will no sense since negative value will translated to another very big positive value.

NSUInteger loc = -1; // equal to 4294967295

1.So, you are in need of a very long string.
2.So, you need to invert your negative value to enormous big value in order to make your example worked

NSString *string = @"abc";
NSRange r1 = NSMakeRange(-NSUIntegerMax, 1);
NSString *myString = [string substringWithRange:r1];
NSLog(@"%@",myString);
beryllium
  • 29,669
  • 15
  • 106
  • 125