0

I have created getName1 & getName2() as below

var myName = "Jhon"
var salt = " Salt"

    
var getName1:String {
    get {
        return myName + salt
    }
}

func getName2() ->String {
    return myName + salt
}

print("getName1\(getName1)")
print("getName2\(getName2())")

So instead of getName2() can I use getName1 directly does that make any difference ?

Dávid Pásztor
  • 51,403
  • 9
  • 85
  • 116
Amit
  • 556
  • 1
  • 7
  • 24
  • For a computed read-only property you can omit the get, and in this case you can omit the return too, like this: var fullName: String { myName + salt } – Shadowrun Jun 08 '21 at 09:43

1 Answers1

2

There is no difference but getName1 is a computed property while getName2 is a function , in this case you better use getName1 as a function is expected to do some block code or IMO not just a one line

Shehata Gamal
  • 98,760
  • 8
  • 65
  • 87