> On Jul 30, 2015, at 1:11 AM, 2551 <[email protected]> wrote:
> 
> In the docs under ‘Special Considerations’ it says "This method detects all 
> invalid ranges (including those with negative lengths). For applications 
> linked against OS X v10.6 and later, this error causes an exception; for 
> applications linked against earlier releases, this error causes a warning, 
> which is displayed just once per application execution.”
> 
> My questions are: what causes this to be an “invalid range” rather than an 
> NSNotFound? And: How do I avoid it when searching for a potentially 
> non-existent string?

You’re confusing -substringWithRange: with -rangeOfSubstring:. The 
documentation and warning you cite come from the former, but you’re talking 
about the latter.

As the docs promise, -rangeOfSubstring: will either return a valid range, or 
{NSNotFound, 0}. What’s probably happening to you is that you called it on a 
nil string. This means the method isn’t called at all. In most cases messaging 
nil will return a value of 0 or 0.0 or nil, but struct returns are an exception 
— the struct will be uninitialized on return, i.e. garbage. (This happens 
because the Obj-C runtime doesn’t know how big the returned struct is, so it 
can’t safely fill it with zeros.)

Put an NSAssert(someString!=nil) before the -rangeOfString: call and it should 
pinpoint when the problem occurs.

—Jens
_______________________________________________

Cocoa-dev mailing list ([email protected])

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to [email protected]

Reply via email to