1

I'm kind new to the Swift language and I'm trying to understand the "self" methods property. I read about it at apple docs and as I understood the using of self-property is when I want to "“refer to the current instance within its own instance methods”. as I read it and the example of apple I understood the using and the reason for it but after that as I look at some tutorial I understood that I didn't really get it. I adding the code of the tutorial and maybe someone can explain me the using according to the code.

override func viewDidAppear(_ animated: Bool) {
super.viewDidAppear(animated)

let width: CGFloat = 240.0
let height: CGFloat = 160.0

let demoView = DemoView(frame: CGRect(x: self.view.frame.size.width/2 - width/2,
                                      y: self.view.frame.size.height/2 - height/2,
                                      width: width,
                                      height: height))

self.view.addSubview(demoView)
}

I really don't get the using of self-property in here, especially because I don't see any "same name" var outside the function. hope someone can help me understand it better. thanks.

A.K.
  • 2,284
  • 3
  • 26
  • 35
omerc
  • 179
  • 1
  • 11
  • 4
    view is an instance of a viewcontroller that is present on all viewcontroller classes. It is not one declared by you. Using self is not required here and is more a matter of style that you or your team prefers. In Swift Self is not generally needed but more a matter of style. – agibson007 Jul 31 '17 at 16:46
  • 1
    Possible duplicate of [What is "self" used for in Swift?](https://stackoverflow.com/questions/26835013/what-is-self-used-for-in-swift) – Shades Jul 31 '17 at 16:48
  • crib notes version is, you basically need it if you are calling a method on self, not generally to access an ivar, unless there is some scope ambiguity... – Grady Player Jul 31 '17 at 17:09

1 Answers1

1

You are correct that, in this case, there is no same-name variable outside the scope, so in this case, there's no need to use self. Let's look at an example where there would be a difference.

class MyClass {

    let myInteger = 6
    let myBool = false

    func doSomething(_ myInteger: Int) {
        print(myInteger, self.myInteger)
    }

}

Then, anywhere else in your code:

let object = MyClass()
object.doSomething(4) // this prints "4, 6" because `myInteger` refers
                      // to the parameter (4), and `self.myInteger`
                      // refers to self's property (6)
Connor Neville
  • 7,291
  • 4
  • 28
  • 44
  • i think that you wrong (fox me if i wrong...), in your example "my integer" refers to the parameter (6) and not to the parameter (4) (this one is self.myinteger). another thing, why do you use _ before the function parameter? – omerc Jul 31 '17 at 17:04
  • `myInteger` refers to the **parameter** (the number passed in via parentheses, which, in this case, is 4) - this is *not* `self.myinteger`. `self.myInteger` refers to the **property** (which, in this case, is 6). You can paste this code into a playground to test it. Using `_` omits the argument label for the parameter, which means I can write `doSomething(4)` instead of `doSomething(myInteger: 4)` when I call the function. – Connor Neville Jul 31 '17 at 17:08
  • thats exactly what i don't get. i thought that the self-property refer to the function parameters, and you saying that the self refer to the class parameters. an i correct? – omerc Jul 31 '17 at 17:24
  • There is no such term as a class parameter. The thing it is referring to is a **property** of the class, or an **instance variable**. The other option, which self does *not* refer to, is a function parameter. – Connor Neville Jul 31 '17 at 17:35
  • so self is just for classes? if i don't use any class, i don't need to use self? – omerc Jul 31 '17 at 20:10
  • `self` references whatever object you are currently in. It's the same for classes, structs, enums. – Connor Neville Jul 31 '17 at 20:15
  • so in your example its work because you set MyClass as an object? was it the same without this declaration? – omerc Jul 31 '17 at 20:20
  • If you just have a top-level function that isn't inside of any object, then there is no notion of `self` inside that function. I'm not sure what other setup you have in mind. – Connor Neville Jul 31 '17 at 20:21
  • sorry for the delay... but i asked if a self is just when i use classes/ struct/ enums or it can be whenever i have some var (somewhere in my code, not specificity in class enums struct) with same name as a var inside a function? – omerc Aug 01 '17 at 19:09
  • You need to be within an object. Again, you can check all of these cases yourself in a playground. – Connor Neville Aug 01 '17 at 19:17