How to declare the same constant "let" so that it exists in several functions within a class?

-1

How do you declare a let constant so that it exists in several functions of the same class and you can call it as self?

That is, in objective-C it was enough to do before @implementation a @property (strong, nonatomic) NSString * variable; and then in the method (function) that we want to use it, call it as self.variable

    
asked by Popularfan 30.08.2017 в 13:17
source

1 answer

3

You must declare it within the class and outside the function, in the following example you can appreciate the class variable "person" and within the functions you can get its value with "self.person" or simply "person"

import Foundation

class MyClass {
    let person = "Harry";

    func hello() -> String {
        return "Hello, " + self.person + "!"
    }

    func bye() -> String {
        return "Bye, " + self.person + "!"
    }

    init() {
        print(hello())
    }
}

MyClass();
    
answered by 30.08.2017 / 14:21
source