Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Is there a tangible benefit to using "self" outside of closures?

Tags:

ios

swift

self

I have noticed a few people in the industry will use the self keyword even when not explicitly required (i.e. outside of closures).

Example:

import UIKit
import MapView
import CoreLocation

class viewController: UIViewController, MKMapViewDelegate, CLLocationDelegate {

    let mapView = MKMapView()
    let locationManager = CLLocationManager()

    override func viewDidLoad() {
        super.viewDidLoad()

        self.mapView.delegate = self
        self.mapView.showsUserLocation = true

        self.locationManager.delegate = self
        self.locationManager.desiredAccuracy = kCLLocationAccuracyBest
    }
}

Is there a tangible benefit to this, runtime-wise? Or is this purely a stylistic choice?

like image 899
Swifty Avatar asked Oct 08 '18 23:10

Swifty


2 Answers

There is.

In the examples you provided it makes no difference, it's purely a style choice. Some people might like it since it explicitly tells you you're modifying self, personally I think it looks cleaner without.

Where it matters is when you have a local variable with the same name. Let's say you have a class that has a var count: Int property. Then, in one of your methods, you declare a new variable with the same name.

The local variable will be used whenever you type count, so if you want to modify or read the object's variable, you'll need to use self.

Some examples where it makes a difference:

guard let count = calculateCount() as? Int else { return }
self.count = count

init(count: Int) {
  self.count = count
}

func updateCount(_ count: Int) {
  self.count = count
}
like image 60
EmilioPelaez Avatar answered Oct 26 '22 23:10

EmilioPelaez


Yes there are some benefits.

  • Using keywords like self or init or Swift prevent Xcode from ambiguity of overloads
  • make it compile faster
  • brings autocomplete suggestions faster;

that's it! Even though you use them explicitly, the compiler removes them from compiled code for compressing reasons. But it is not preferred! It's all about the scope of variables and functions.

Take a look at this example:

let name = "global variable"
class MyClass {
    let name = "object variable"

    func testScopes() {
        let name = "function local variable"
        print(name) //prints: function local variable
        print(self.name) //prints: object variable
        print(MyProject.name) // prints: global variable
    }
}

let myObject = MyClass()
myObject.testScopes()

Here are three variables with three different valid scopes. You can refer to each one you need differently. Swift community suggested:

For conciseness, avoid using self since Swift does not require it to access an object's properties or invoke its methods.

Use self only when required by the compiler (in @escaping closures, or in initializers to disambiguate properties from arguments). In other words, if it compiles without self then omit it.

But it's ultimately up to you and your company's code style guide.

like image 37
Mojtaba Hosseini Avatar answered Oct 26 '22 23:10

Mojtaba Hosseini