Roval
Roval

Reputation: 597

Difference between variables in interface Object() {} and @implementation Object @end

I'm starting my adventure with Objective-C and iOS and I've got one thing that I don't know how to use correctly and this is literally blowing my mind.

Many tutorials have private class variables in .m files defined like this:

@interface ViewController (){
    @property (nonatomic, strong) NSMutableArray *myArray;
}

or like this:

@implementation ViewController
        NSMutableArray *myArray;
@end

In the first example I can use _myArray instead of self.myArray, which I like, but should I put all my private variables in interface files? What's the difference between those two variables? When should I use one instead of another, and which is safer?

Upvotes: 1

Views: 987

Answers (5)

Rob
Rob

Reputation: 437532

A couple of thoughts:

  1. The first example is not syntactically correct. You probably meant the following, which defines a declared property inside the class extension:

    @interface ViewController ()
    @property (nonatomic, strong) NSMutableArray *myArray;
    @end
    

    A property will:

    • Synthesize an instance variable called _myArray (or if you specify a @synthesize directive, you can control the name of this instance variable);

    • Synthesize accessor methods, notable a myArray getter that retrieves the value and a setMyArray setter that sets the value;

    • Provide other features such as key-value coding, etc.

  2. On the other hand, the following declares a global variable:

    @implementation ViewController
    NSMutableArray *myArray;
    @end
    

    Globals are a very different beast, shared amongst all of the various instances of this class (and across the whole app). In this case (some mutable array used by a class instance), a global is likely not what you intended.

  3. If you intended to define an instance variable, you could do:

    @implementation ViewController
    {
        NSMutableArray *myArray;
    }
    @end
    

    Or, perhaps better than defining this ivar in the @implementation like that, one would generally define them within the class extension's @interface:

    @interface ViewController ()
    {
        NSMutableArray *myArray;
    }
    @end
    

I suspect you didn't actually intend to compare the global variable to a instance variable (ivar) or property, but rather were asking the rationale for privately using a property vs. ivar within a class implementation:

Bottom line, within a particular class, using ivars is a perfectly acceptable practice, but many of us use private properties defined in class extensions. The overhead is minimal and it abstracts the code away from the implementation details of the ivar. For example, you can customize one or more of the accessor methods at some future date and have minimal impact on the rest of the class implementation. But it's a matter of personal preference.

Upvotes: 1

iBhavin
iBhavin

Reputation: 1261

The difference is that:

  • _myArray is instance variable.

  • self.myArray is calling a getter method on your object.

  • Using self.myArray = nil makes the variable go through its setter and therefore release the object when ARC is not used).

    • If the property is declared with atomic (default value) which means access the variable is thread-safe with the cost of performance
    • nonatomic property means race condition can happen when access the variable or property from multiple threads.

In general, use atomic for object shared with multiple threads and nonatomic for UI or not shared object.

Upvotes: 3

Duyen-Hoa
Duyen-Hoa

Reputation: 15784

Attention, you will get compiler error with your code:

@interface ViewController (){
    @property (nonatomic, strong) NSMutableArray *myArray;
}

-> you must move @property... outside of {} of your header.

@interface ViewController (){
    //
}

@property (nonatomic, strong) NSMutableArray *myArray;

Upvotes: 2

Luvie
Luvie

Reputation: 126

yes, @property is automatically creates setter and getter.

additionally, you can setting property's attribute. (read-only/readwrite, nonatomic/atomic, strong/weak.. etc)

accessing instance variable by getter & setter(instead of using pointer to direct access) make data encapsulated.

it is common and important concepts of Object-Oriented Programming.

read this for understanding.

https://developer.apple.com/library/ios/documentation/Cocoa/Conceptual/ProgrammingWithObjectiveC/EncapsulatingData/EncapsulatingData.html

sorry for poor english. :<

Upvotes: 0

Kets
Kets

Reputation: 468

@property creates your setters and getters the other one does not.

Upvotes: 0

Related Questions