Ben_hawk
Ben_hawk

Reputation: 2486

Objective-c Differences

What the difference between declaring an @property in .h or .m file

@property (nonatomic, readwrite, assign) BOOL notificationDidLaunch;

Is it to do with the scope of the variable?

Also in the .h file whats the difference between declaring a string with the brakets like so

@interface AppDelegate : NSObject < UIApplicationDelegate > {
    NSString *hat;
}

and doing it outside of them like below

@property (nonatomic, strong) NSString *hat;

Upvotes: 0

Views: 113

Answers (2)

Andrew Madsen
Andrew Madsen

Reputation: 21383

As Tiago says, putting an @property declaration inside a class extension in the .m (implementation) file, is a way to make the property private so only the class itself can access it. When declared in the .h (public interface file), it is visible to all code that imports that .h file. Keep in mind that @properties are really just a convenience for declaring and synthesizing accessor methods, and as with all methods in Objective-C, they're never truly private. The best you get is a compiler warning that no public interface declares the method in question if you try to use a non-public method in another class.

For the second part of your question, this declares an instance variable ("ivar") called myString:

@interface MyClass : NSObject
{
    NSString *myString;
}

While this declares a property called myString:

@property NSString *myString;

The difference between an instance variable and an @property is more significant than just saying that an ivar is accessible only by your class's instances. Declaring an ivar adds a variable to a class's structure in memory. In contrast, @properties declare/define methods on a class. By default, these methods set/get the value of an associated, and similarly named ivar, but that's not a requirement, and it is perfectly acceptable and quite common to have methods for an @property that don't access an ivar directly. Say for example a class that has a firstName and lastName properties backed by _firstName and _lastName ivars, along with a third, fullName property that simply concatenates the value returned by the firstName and lastName getter methods together (and/or splits a two part name in its set method).

Upvotes: 2

Tiago Almeida
Tiago Almeida

Reputation: 14237

A summarized explanation

A @property inside the .m is private to the class and in the .h is public.

The difference between declaring inside brackets or without brackets is the meaning of the variable.

Inside the brackets you declare an instance variable (or ivar) that is meant to be acessible only by your classes' instances. A property (declared in the .h) is meant to be acessible by any class.

Properties

A @property essentially defines a set and a get that you can override. When you do:

AppDelegate myAppDelegate;
myAppDelegate.hat = @"A hat": 

You are essencially doing:

[myAppDelegate setHat:@"A hat"]

And when you do

myAppDelegate.hat //so you can get the property's value

you are essencially doing

[myAppDelegate hat]

Overriding Sets and Gets

When you do @synthesize hat = _hat you are essentially creating a get and a set that has an instance variable _hat associated. This instance variable should only be accessed in your gets/sets and even inside your class you should use your sets/gets (using self.property)

You can override the set and get created by the @synthesize overriding the following methods:

- (void) setHat:(NSString*) aHat
- (NSString *) hat

Upvotes: 2

Related Questions