1

An issue I have (maybe it is really easy to solve, but I am new to Obj-C. I want to print out the location and length of NSRange I created, but I get an error message. Here the code

NSRange tmpRange = [newData rangeOfData:segmentToFind options:kNilOptions range:NSMakeRange(0u, [newData length])];

NSLog(@"%@ - range location : %lu", [tmpRange location];

The error message I get is:

Bad receiver type 'NSRange' (aka 'struct _NSRange')
rookieOC
  • 47
  • 5

2 Answers2

4

To solutions:

A. You have to log each component separately:

NSLog( @"%lu - %lu", (long)range.location, (long)range.length );

B. You can convert it to an instance of NSString and then log the string:

NSLog( @"%@", NSStringFromRange(range) );

Output

{1, 2}

NSStringFrom…() is similar to -description and exists for many types including NSSize, NSPoint.

Amin Negm-Awad
  • 16,582
  • 3
  • 35
  • 50
1

NSRange starts with the letters NS, but it isn't an NSObject. It is an ordinary struct, like a struct in C or in C++. So to access location and length, you just use range.location and range.length. There are other structs like that, like NSRect, NSPoint, NSSize.

You noticed already what the compiler said: NSRange is a typedef for the type "struct _NSRange { ... }".

And then there are types like NSInteger and NSUInteger which also start with NS but which are really just primitive types (int or long, unsigned int or unsigned long).

gnasher729
  • 51,477
  • 5
  • 75
  • 98