0

What are the differences between Instances and member variables in the context of objective-c. I have seen these two terms referred to but never clearly delineated. A google and stack overflow search yielded no immediate explanations so i thought I'd pose the question.

1
  • 1
    Instance variables and member variables are two words meaning the same thing. Really in Objective-C you usually mostly deal with properties, which autogenerate instance/member variables. Commented Aug 6, 2015 at 3:51

1 Answer 1

1

Member variable is a more generic term. In other languages, like C++ or Java, member variable can refer to either an instance variable or a class variable (static member variable).

Objective C does not have class variables, so instance variable and member variable are synonyms.


As a side note, in modern Objective C, instance variable (also called ivars in Objective C) should only be created from auto-synthesized properties (as noted by rfj001).


UPDATE

Instance variable only being created from auto-synthesized properties is an Apple recommended practice for modern Objective C.

Adopting Modern Objective-C

Using properties instead of instance variables in as many places as possible provides many benefits

Programming with Objective-C: Encapsulating Data

It’s best practice to use a property on an object any time you need to keep track of a value or another object.

As with all rules of programming there may be times when you need to break this rule and directly declare an ivar. This however should be the rare exception and not the rule.

Sign up to request clarification or add additional context in comments.

2 Comments

Please explain why you think ivars should only be synthesized from properties. What's wrong with have non-property ivars?
@rmaddy I think the question should be what's good about an ivar without a property to encapsulate it? Abstracting data access was a major step forward in object oriented programming.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.