Michael
Michael

Reputation: 591

When do I use the self property?

I am trying to better grasp the use of self. Is using self basically just a shortcut to typing out the class name? For example, in the code below, if I use self does it refer to the Tank2 class? For example, is it basically a shortcut to typing out Tank2.bonusDamage? I don't understand what "the current instance" is that self is referring to.

I don't quite grasp it because I notice for example that these top two code snippets return the exact same thing.

    //SNIPPET 1:
    var damage: Double {
    return baseDamage + Tank2.bonusDamage  
}

    //SNIPPET 2:
    var damage: Double {
    return self.baseDamage + Tank2.bonusDamage

}

.

class Tank2 {
class var bonusDamage: Double {
    return Double(Upgrade.level) * 2.5
}

let baseDamage = 10.0

var damage: Double {
    return baseDamage + Tank2.bonusDamage
}

class func upgrade() {
    Upgrade.level += 1

}

struct Upgrade{
static var level = 0
}
}

Upvotes: 0

Views: 144

Answers (3)

Paulw11
Paulw11

Reputation: 115103

A class definition describes the properties and methods of the class like the plans for a car describe its size, number of seats and so on - but you can't drive plans, you need an actual instance of a car built from those plans.

Similarly with an object class - you need an actual instance of a class to operate on.

Given a simple Car class -

Class Car
   property color:Color
   property fuelTankLevel:Integer
   propert (readOnly) capacity:Integer = 60 // Fuel tank capacity in litres

Note that this isn't Swift or Objective C, just a simplified version of a class.

I could store instances in variables -

 let redCar=Car(color:Red)
 let blueCar=Car(color:Blue)

which is fine for referring to those cars from within my program, but what about a method of car that let me fill the fuel tank. From my program I can say blueCar.fill() - but inside the fill method I need to operate on the right tank. Inside fill I can't refer to blueCar and redCar because these are just instances outside of the class definition. This is where self comes in.

self means "the current object instance", so the fill method could be

fill() {
    self.fuelTankLevel=self.capacity
}

So when this code is invoked on an object instance, self will be that instance. So when I say blueCar.fill() the code that is executed is effectively blueCar.fuelTankLevel=blueCar.capacity.

In Swift you can omit self and in Objective-C you can use _fuelTank (or whatever backing variable you @synthesized) to access the property backing variable directly, but it is a good idea to get into the habit of using self as it makes it clear that you are accessing a property and not a local or global variable. Also in Objective-C it ensures that any setter/getter methods are invoked.

You can define Class methods - these are methods that do not operate on any particular instance. In Objective-C you often see "constructor" class methods that wrap the two-stage alloc/init into a single Class method so instead of saying:

 Car *redCar=[[Car alloc]initWithColor:red];

You can say

 Car *redCar=[Car carWithColor:red];

Upvotes: 1

matt
matt

Reputation: 536027

In class code - such as a class method, a static method, or the initializer of a class property - self means "this class, whatever it may be".

In instance code - such as an instance method, or the initializer of an instance property - self means "this instance, whatever it may be".

But in most situations, self can be omitted - because if a message is a sent without a recipient, Swift will assume that you intend to send it to self.

In your example, baseDamage is an instance variable, and the phrase return self.baseDamage + ... appears in instance code. Thus, you can say baseDamage or self.baseDamage interchangeably, because Swift assumes that the bare message baseDamage means self.baseDamage.

Upvotes: 0

swapnilagarwal
swapnilagarwal

Reputation: 1174

No self is not same as typing a class name.

self is a object of current class. So all the properties and functions values which are available in that class can be references using self.

However, there are functions (called Class functions and distinguised with +), that are not dependent on a class object. These Class Functions are called via class name. Example

Class TestClass
+ (int)addToFive:(int)input
{
   return input+5;
}

Call this method [TestClass addToFive:10], will return 15.

Class TestClass
@property int counter;

-(int)addToCounter:(int)input
{
   // self.counter corresponds to counter value for self (this particular TestClass object)
   return self.counter + input;
}

// call this method
TestClass *classObject = [TestClass alloc] init];
classObject.counter = 10;
[classObject addToCounter:10] // will return 20.

I hope this helps you.

Upvotes: 0

Related Questions