What is the difference between declaring variables in the interface section vs. in the implementation section outside of a method.
What is the difference between declaring variables in the interface section vs. in the
Share
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.
Do you mean in the ivar block in the implementation, as in
If so, then the only difference is visibility to other code. At runtime, the ivar will be indistinguishable from one declared in the
@interfacesection. However, code outside the class can see ivars declared in@interfaces, and unless those ivars are marked with@protectedor@private, then the other classes can reach in and twiddle the ivars. But ivars declared in the@implementationare not even visible to code outside, so they cannot touch the ivars.For the most part, this is just a code cleanliness issue. Nothing should go in the header file unless it’s meant to be public. So why put ivars there?
As Josh Caswell noted, ivars declared in this fashion require a recent version of Clang.
The alternative interpretation of your question is you have code like
If this is what you meant, then the answer is, don’t do that. In this code snippet we’ve declared a global variable named
varrather than an instance variable. The location of the variable inside the@implementationblock is irrelevant, it’s exactly identical to a global variable in C (because that’s what it is).