The self keyword in initWithDelegate:queue

Hi,

I had a question about the usage of the self keyword in general and in specific context to the Core Bluetooth method initWithDelegate:queue:


Firstly,


What is the meaning of the self keyword? From my understanding it is referring to the ViewController that I am currently working in. For example, if I have a property called someTextField inside my ViewController.h file, When I access the property I say: self.someTextField. So from that I understand that self is the viewcontroller. Is this the right way on thinking about it?


With regards to the initWithDelegate:queue method. This is what I have:

self.deviceManager = [[CBCentralManager alloc]initWithDelegate:self queue:nil];


So in the developer doumentation, the first parameter is the delegate whihc receives the events. In this case, it is set to self. So is self reffering to the ViewController, or the CBCentralManagerDelegate that the ViewController has adoped or is it referring to something else entirely.


Any help would be appreciated!

Thank you

What is the meaning of the self keyword?

For code inside an instance method of class X, it’s a reference to the instance of class X (or subclass of class X) on which that method was called. For code running inside a class method of class X, it’s a reference to class X (or subclass of X).

From my understanding it is referring to the ViewController that I am currently working in. For example, if I have a property called someTextField inside my ViewController.h file, When I access the property I say: self.someTextField. So from that I understand that self is the viewcontroller. Is this the right way on thinking about it?

Yes (assuming that the code you’re talking about is an instance method of the ViewController class).

With regards to the initWithDelegate:queue method. … So in the developer doumentation, the first parameter is the delegate whihc receives the events. In this case, it is set to self. So is self reffering to the ViewController … ?

Yes (with the same assumption as above).

Share and Enjoy

Quinn “The Eskimo!”
Apple Developer Relations, Developer Technical Support, Core OS/Hardware

let myEmail = "eskimo" + "1" + "@apple.com"

and:


>So is self reffering to the ViewController, or the CBCentralManagerDelegate that the ViewController has adoped


It is referring to the ViewController that has adopted the CBCentralManagerDelegate protocols

The self keyword in initWithDelegate:queue
 
 
Q