Say I have a method that takes a CLLocationCoordinate2D. I can’t pass nil directly to that in code; the compiler complains. However, the following compiles. So what happens at runtime?
CLLocation* loc = nil;
[self passMeCoordinates:loc.coordinate];
Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.
Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.
Lost your password? Please enter your email address. You will receive a link and will create a new password via email.
Please briefly explain why you feel this question should be reported.
Please briefly explain why you feel this answer should be reported.
Please briefly explain why you feel this user should be reported.
This is an interesting question. I assume your earlier code was:
The compiler complains about this because CLLocationCoordinate2D is not an object. It’s a C-style struct. So, you can’t pass an object/pointer (nil) where a struct is expected. (They’re not the same size, etc.)
If I slightly paraphrase your code to:
The question comes down to “what value does
coordhave”. As you may know, the rule in Objective-C is that if you send a message to nil (as we do here — loc.coordinate is equivalent to [loc coordinate]), then you get back 0. But what if we’re expecting a struct? As I just mentioned, it’s not the same size as an integer. Well, it turns out that the result depends on what compiler you’re using:LLVM 3.0+ (Xcode 4.2+): returns all zeros, so it’s equivalent to a coordinate of (0,0): Can I rely on nil UIView returning CGRectZero for its frame?
LLVM Earlier/GCC: a struct can be filled with garbage/undefined contents, so it could be anything.