Difference Between Declaring a Variable Under @Implementation And @Interface Under .m file

JLT

I have been learning Objective-C for awhile. From what I learned, I know that when you declare a variable inside @interface in the .h file, the variable can be accessed publicly(similar as a public variable in java).

@interface MyObject

@property NSInteger intData;

@end

But when you declare it inside @interface in the .m file. It can only be accessed inside the .m file under @implementation only, unless you provide a getter and setter for it.

@interface MyObject ()

@property NSInteger intData;

@end

But I also noticed another way of declaring a variable, which is declaring it under @implementation

@implementation

NSInteger intData;

@end

and I see that it works the same way as declaring it under @interface with @property in .m file

I don't understand the difference between the two(declaring under @implementation and under @interface(in .m file).

I've already searched through stack about this, but they were all talking about the difference between @implementation and @interface(in .h file). So think this is not a duplicate.

iAdjunct

First off, you're not declaring a variable; you're declaring a property. A property is backed by an instance-variable, but it also adds methods. Here's an explanation of the places to put variables:

@interface MyClass : NSObject {
    NSInteger i ;
}
@end

This is a place to put an instance variable on your class. It is only accessible by methods of your class and categories. (Sidenote: it CAN be made accessible externally, but that's not a recommended practice)

Another example:

@interface MyClass : NSObject
@end

@implementation MyClass {
    NSInteger i ;
}
@end

This is also an instance variable, but is only accessibly by methods written inside that block. (Sidenote: it can be accessed by digging through the class definition, but that's not a recommended (or common) practice)

Another example:

@interface MyClass : NSObject
@property NSInteger i ;
@end

Is the same as:

@interface MyClass : NSObject {
    NSInteger _i ; // you are not allowed to access it by this variable
}
- (NSInteger) i ;
- (void) setI:(NSInteger)value ;
@end

This is a property people are allowed to get and set. You use that variable in your methods or in other methods as:

NSLog ( @"The value is %i" , self.i ) ; // if it's your instance method
NSLog ( @"The value is %i" , object.i ) ; // if it's object's instance method

Another example:

@interface MyClass : NSObject {
    NSInteger i ;
}
@property NSInteger i ;
@end
@implementation MyClass
@synthesize i ; // Causes the property to line up with the ivar by the same name.
@end

Is the same as:

@interface MyClass : NSObject {
    NSInteger i ; // you ARE allowed to use this since you defined it
}
- (NSInteger) i ;
- (void) setI:(NSInteger)value ;
@end

Here, you can use the getter/setter methods or the instance variable itself. However, you should generally use the methods because you [implicitly] declared them atomic so they have threading synchronization. If you want to make it NOT do threading (and speed it up, as long as you're not going to use it in a multi-threaded environment):

@property (nonatomic) NSInteger i ;
@property (nonatomic,readonly) NSInteger i ; // only makes a getter method

I'd recommend avoiding this for a while and use the straight properties because it'll help you avoid a lot of common mistakes. Unless you profile your program and determine that this is a cause of a performance loss, you should probably simply use the properties.

Another example:

@interface MyClass : NSObject
@end

@implementation MyClass
NSInteger i ;
@end

This is NOT an instance variable. It is a global variable that happens to have been written inside your @implementation scope.

See above for how to turn this into an instance variable (i.e. putting it in braces).

One more note:

Declaring a property like this:

@interface MyClass ()
@property NSInteger i ;
@end

Doesn't make it private. However, it is hidden in a file that people generally can't access so the compiler doesn't know a property exists.

Other functions elsewhere in your code CAN still call:

[yourObject i] ;

To get the value of that property - but they have to know it's there first.

Addendum to answer a question in the comments:

Properties are, by default, atomic. It doesn't necessarily follow the strict definition of atomic (this is a can of worms I suggest you not look at right now), but has the same effect: threads are guaranteed to see a complete and up-to-date value, regardless of when another thread writes to it. It generally does this when it synthesizes the getter/setter methods:

- (NSInteger) i {
    @synchronized(self) {
        return i ;
    }
}
- (void) setI:(NSInteger)value {
    @synchronized(self) {
        i = value ;
    }
}

If you instead specify nonatomic, it'll synthesize these:

- (NSInteger) i {
    return i ;
}
- (void) setI:(NSInteger)value {
    i = value ;
}

If your property is atomic, then you shouldn't ever access the ivar directly. Doing so violates the threading protection you gave it to begin with. (Sidenote: there are cases where you can, but wait until you become more familiar with threading/synchronization before you attempt it.)

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Difference Between Declaring a Variable Under @Implementation And @Interface Under .m file

From Dev

Difference between declaring instance variable in .h file and .m inside the @interface braces

From Dev

Difference between declaring instance variable in .h file and .m inside the @interface braces

From Dev

What is the difference between declaring a member in the extended interface versus in the implementation?

From Dev

What is the difference between declaring a member in the extended interface versus in the implementation?

From Dev

Difference between declaring variable

From Dev

Difference between declaring a variable with and without get; set;

From Dev

Difference between declaring a variable in ios Swift?

From Dev

The difference between version and build under Identity

From Dev

The difference between version and build under Identity

From Dev

What is the difference between declaring a variable directly and using the keyword new?

From Dev

Declaring variable (difference between int c; and int c=new int();

From Dev

What is the difference between declaring a final variable in an Activity and instantiating it on the onCreate method?

From Dev

What is the difference between using var and not when declaring a variable?

From Dev

JAR Manifest file - Difference between Specification and Implementation

From Dev

What is the difference between push and offset under the grid system?

From Dev

Difference between Multiple Response Sets Under "Data" and "Analyze" Menus in SPSS

From Dev

Find the difference between the dates and group it under some category

From Dev

What is the difference between jre folder under JDK folder and jre folder?

From Dev

What is the difference between the C programming language and C programming under linux?

From Dev

Difference between users listed under home directory and those not

From Dev

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

From Dev

In PHP, what is the difference between declaring a variable as global inside function, or passing the variable as an argument to the function?

From Dev

What is the implementation difference between static variable and static field?

From Dev

Anonymous functions - what's the difference between declaring a global variable and use in php?

From Dev

What's the difference between declaring a final static variable on Superclass doing so on the extendedClass?

From Dev

awk to store values under keyword in variable from file

From Dev

How to access a text file stored under a variable name

From Dev

batch file difference between %variable% and %variable % or variable call with spaces

Related Related

  1. 1

    Difference Between Declaring a Variable Under @Implementation And @Interface Under .m file

  2. 2

    Difference between declaring instance variable in .h file and .m inside the @interface braces

  3. 3

    Difference between declaring instance variable in .h file and .m inside the @interface braces

  4. 4

    What is the difference between declaring a member in the extended interface versus in the implementation?

  5. 5

    What is the difference between declaring a member in the extended interface versus in the implementation?

  6. 6

    Difference between declaring variable

  7. 7

    Difference between declaring a variable with and without get; set;

  8. 8

    Difference between declaring a variable in ios Swift?

  9. 9

    The difference between version and build under Identity

  10. 10

    The difference between version and build under Identity

  11. 11

    What is the difference between declaring a variable directly and using the keyword new?

  12. 12

    Declaring variable (difference between int c; and int c=new int();

  13. 13

    What is the difference between declaring a final variable in an Activity and instantiating it on the onCreate method?

  14. 14

    What is the difference between using var and not when declaring a variable?

  15. 15

    JAR Manifest file - Difference between Specification and Implementation

  16. 16

    What is the difference between push and offset under the grid system?

  17. 17

    Difference between Multiple Response Sets Under "Data" and "Analyze" Menus in SPSS

  18. 18

    Find the difference between the dates and group it under some category

  19. 19

    What is the difference between jre folder under JDK folder and jre folder?

  20. 20

    What is the difference between the C programming language and C programming under linux?

  21. 21

    Difference between users listed under home directory and those not

  22. 22

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

  23. 23

    In PHP, what is the difference between declaring a variable as global inside function, or passing the variable as an argument to the function?

  24. 24

    What is the implementation difference between static variable and static field?

  25. 25

    Anonymous functions - what's the difference between declaring a global variable and use in php?

  26. 26

    What's the difference between declaring a final static variable on Superclass doing so on the extendedClass?

  27. 27

    awk to store values under keyword in variable from file

  28. 28

    How to access a text file stored under a variable name

  29. 29

    batch file difference between %variable% and %variable % or variable call with spaces

HotTag

Archive