views:

379

answers:

1
  NSString *foo = @"     x   ";
  NSRange r = [foo rangeOfCharacterFromSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
  NSLog(@"foo range = %d,%d",r.location, r.length);

Results in "foo range = 0,1"

So will it ever return a length > 1?

+5  A: 

See the documentation.

Peter Hosey
It may be helpful here to note that the reader should consult rangeOfCharacterFromSet:options:range: to understand the full details and why length can be greater than 1. The linked function implies you need to keep reading (and then keep reading again since the reference method points to to yet another), but may be confusing to those unfamiliar with Apple's doc style.
Rob Napier
Ah, good catch. I thought it said “length that isn't 1”, not “length > 1”. I read too fast, I think.
Peter Hosey
Thanks - I had actually read the documentation for the function I was using, and had not continued to read, so I didn't see the "discussion" note in rangeOfCharacterFromSet:options:range:.
LeftHem