I’ve recently started using Ick’s ‘andand’ for Ruby so I can iterate through nested collections more easily than before.
Is there a version of this idiom for Objective-C implemented anywhere?
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.
I’m not a Ruby programmer, so I may be missing something subtle, but it appears that andand makes it so that you can safely chain method calls even when one method call may return nil. This is built in in Objective-C, because calling methods on (actually properly termed sending messages to) nil is safe and perfectly acceptable. Messages to nil always return nil, and are basically a no-op. In Objective-C this is fine:
Therefore, this is fine too, even if objectByDoingSomething might return nil:
From The Objective-C Programming Language: “In Objective-C, it is valid to send a message to nil—it simply has no effect at runtime.” That document contains more detail about the exact behavior of calling methods on nil in Objective-C.