What is the difference between _variable & self.variable in Objective-C?

I am new to Objective-C and iOS, currently trying to learn application development using the iOS 6 SDK. One concept that I really can't wrap up is the difference between "_variable" and "self.variable" when accessing the .m file. They are the same? Or different?

Below is a simple example.

Myclass.h

#import <Foundation/Foundation.h>

@interface MyClass : NSObject
@property (strong, nonatomic) NSString *myName;
@end

Myclass.m

#import "MyClass.h"

@interface MyClass ()
@property (nonatomic, strong) NSString *anotherName; 
@end

@implementation MyClass
- (void) myFunction {
    _myName = @"Ares";
    self.myName = @"Ares";

    _anotherName = @"Michael";
    self.anotherName = @"Michael";
}
@end

So, is there a difference in the above implementations for setting a variable? The variable "myName" is public, and "anotherName" is private.

I would be very grateful for any materials. Thank!

+5
source share
1 answer

The difference is that:

_ .

self.variable getter .

, .

, ARC -

self.variable , retain strong _variable

+13

All Articles