NSRange strange behavior
I have strange NSRange behavior on my device (iPhone iOS 4.3). This code has a different behavior on device and simulator.
for (Location *location in locationArray)
{
NSRange range1 = [location.name rangeOfString:searchText options:NSCaseInsensitiveSearch];
NSRange range2 = [location.streetAddress rangeOfString:searchText options:NSCaseInsensitiveSearch];
NSRange range3 = [location.postalCode rangeOfString:searchText options:NSCaseInsensitiveSearch];
if (range.length1 > 0 | range2.length > 0 | range3.length > 0)
[self.filteredList addObject:location];
}
If some of prop开发者_如何学JAVAerties has nil value range.lenght for that property is equal to 2 on device and zero in simulator.
Am I doing something wrong?
The Objective-C Programming Language
The value returned from a message to nil may also be valid:
- If the method returns an object, then a message sent to nil returns 0 (nil). For example: Person *motherInLaw = [[aPerson spouse] mother]; If the spouse object here is nil, then mother is sent to nil and the method returns nil.
- If the method returns any pointer type, any integer scalar of size less than or equal to sizeof(void*), a float, a double, a long double, or a long long, then a message sent to nil returns 0.
- If the method returns a struct, as defined by the Mac OS X ABI Function Call Guide to be returned in registers, then a message sent to nil returns 0.0 for every field in the struct. Other struct data types will not be filled with zeros.
- If the method returns anything other than the aforementioned value types, the return value of a message sent to nil is undefined.
It's for Mac OS X but I think it's safe to say that you should not rely on returned struct values if message target is nil.
精彩评论