gran_profaci
gran_profaci

Reputation: 8473

Objective C String Comparison. Weird Behavior?

I'm just doing a plain old string comparison for an if condition and I got a weird behavior. In the console, I got the following and fell off my seat.

(lldb) po [@"Puc X-1234" rangeOfString:@"Puc"]
nil

How can this be?

Upvotes: 0

Views: 68

Answers (1)

Bryan Chen
Bryan Chen

Reputation: 46608

rangeOfString: return NSRange, which is not an object but po is for print objc object

try p (NSRange)[@"Puc X-1234" rangeOfString:@"Puc"]

the reason it print nil is that the returning NSRange is (0,3), then the debugger take the 0 part and assume it is id type, and print nil

Upvotes: 6

Related Questions