3

I am new to iOS development, and study about Bluetooth Low Energy (BLE, Bluetooth 4.0) for IOS.

I have see some example code like the following:

@property (strong, nonatomic) CBPeripheralManager *peripheralManager;

- (void)viewDidLoad
{
    [super viewDidLoad];

    // Start up the CBPeripheralManager
    _peripheralManager = [[CBPeripheralManager alloc] initWithDelegate:self queue:nil];
}

The question is :

Why it add the "_" before peripheralManager in ViewDidload ?

Sorry about my English and any stupid...

Thanks in advance.

Amar
  • 13,202
  • 7
  • 53
  • 71
Wun
  • 6,211
  • 11
  • 56
  • 101
  • http://stackoverflow.com/questions/822487/how-does-an-underscore-in-front-of-a-variable-in-a-cocoa-objective-c-class-work – Larme Apr 17 '14 at 08:00
  • this is also a very commonly used style in many other programming languages – Bryan Chen Apr 17 '14 at 08:17
  • @BryanChen - Just to say this has been marked as a duplicate but the answer you are linking to was to a question asked in 2009 and the accepted answer doesn't match the updated compiler/syntax of Objective C – Flexicoder Apr 17 '14 at 08:40

3 Answers3

7

From the Apple Docs

Most Properties Are Backed by Instance Variables By default, a readwrite property will be backed by an instance variable, which will again be synthesized automatically by the compiler.

An instance variable is a variable that exists and holds its value for the life of the object. The memory used for instance variables is allocated when the object is first created (through alloc), and freed when the object is deallocated.

Unless you specify otherwise, the synthesized instance variable has the same name as the property, but with an underscore prefix. For a property called firstName, for example, the synthesized instance variable will be called _firstName.

Although it’s best practice for an object to access its own properties using accessor methods or dot syntax, it’s possible to access the instance variable directly from any of the instance methods in a class implementation. The underscore prefix makes it clear that you’re accessing an instance variable rather than, for example, a local variable:

- (void)someMethod {
    NSString *myString = @"An interesting string";

    _someString = myString;
}

n this example, it’s clear that myString is a local variable and _someString is an instance variable.

In general, you should use accessor methods or dot syntax for property access even if you’re accessing an object’s properties from within its own implementation, in which case you should use self:

- (void)someMethod {
    NSString *myString = @"An interesting string";

    self.someString = myString;
  // or
    [self setSomeString:myString];
}

The exception to this rule is when writing initialization, deallocation or custom accessor methods, as described later in this section.

Flexicoder
  • 8,251
  • 4
  • 42
  • 56
  • You say `the synthesized instance variable has the same name as the property, but with an underscore prefix`. Why the name of `_someString` and `myString` are difference ? – Wun Apr 17 '14 at 11:17
  • they are just examples of how the `someString` property/instance variable are set - this information is straight from the Apple documentation – Flexicoder Apr 17 '14 at 12:12
0

It's class member variable's internal access convention.

@property (strong) NSString *testString;

It can be access in class, self.testString or _testString. But it is different meaning. self.testString means access through auto created member function. But _testString means access direct to variable.

hjpark
  • 11
  • 1
  • 2
0

Compiler automatically generates instance variable _peripheralManager when you declare property @property (strong, nonatomic) CBPeripheralManager *peripheralManager;

You could declare it manually, result would be the same:

@synthesize peripheralManager = _peripheralManager;
Vlad Papko
  • 13,184
  • 4
  • 41
  • 57