This is the 12th day of my participation in Gwen Challenge

Methods are functions associated with a particular type.

The main difference between Swift and OC about methods is that in Swift, both structures and enumerations can define methods.

Instance methods

  • Instance methods

    • Instance method: A function that belongs to an instance of a particular class, structure, or enumerated type
    • Instance methods have the same syntax as functions
    • Instance methods have implicit access to all other instance methods and properties of that type, and instance methods can only be called on specific instances of their type
    class Student {
        var name = ""
        func gotoSchool() {
            print("\(name) Go to school")}}let stu = Student()
    stu.name = "Li lei"
    stu.gotoSchool()
    
    logLi Lei goes to schoolCopy the code
  • The self attribute

    • Each instance of a type has an implicit attributeself, which is equivalent to the instance itself. useselfProperty refers to the current instance object in its own instance method
    • In most cases, it is not needed in instance methodsselfProperty to reference the instance’s property or call another method, because Swift automatically infer that it is an instance of the current type. However, if an instance method parameter name is the same as the instance attribute name, the parameter name takes precedence. If an attribute name is required, the parameter name is requiredselfTo distinguish parameter names from attribute names
    class Student {
        var name = ""
        func sayHI(name :String) {
            print("hello \(name),I am \(self.name)")}}let student = Student()
    student.name = "Li lei"
    student.sayHI(name: "hanmeimei")
    
    log:
    hello hanmeimei,I am Li lei
    Copy the code
  • Instance method to modify the value type

    Structs and enumerations are value types. By default, properties of value types cannot be modified in sample methods. If you need to modify its properties in a particular method, you can use the keyword mutating, which precedes the func keyword of the instance method defined in an enumeration or structure. When the method ends, any changes made are written back into the original data structure. The method can also assign an entirely new instance to its implicit self attribute, and the new instance represented by self will replace the existing instance at the end of the method

    • The structure of the body
    struct Student {
        var name = ""
        mutating func changeName(name:String) {
            // Assign to the implicit self attribute
            self = Student.init(name: name)
        }
    }
    
    var student = Student.init(name: "lilei")
    print(student.name)
    
    student.changeName(name: "hanmeimei")
    print(student.name)
    
    log:
    lilei
    hanmeimei
    Copy the code
    • The enumeration
    enum Color{
        case red
        case yellow
        case green
    
        mutating func changeColor() {
            switch self {
            case .red:
                self = .yellow
            case .yellow:
                self = .green
            case .green:
                self = .red
            }
        }
    }
    
    var color = Color.red
    print(color)
    color.changeColor()
    print(color)
    
    log:
    red
    yellow
    Copy the code

Type method

  • Type method: a method called using the type itself rather than an instance of the type.

  • Func: static func: static func: static func: static func: static func: static func: static func: static func: static func: static func: static func

  • In type methods, the implicit self attribute refers to the type itself rather than to an instance of the type. You can use the self attribute to disambiguate class attributes and type method parameters

    class Student {
        static var name = "hanmeimei"
        class func sayHI(name :String) {
            print("hello \(name), I am \(self.name)")
        }
    }
    
    Student.sayHI(name: "lilei")
    
    log:
    hello lilei, I am hanmeimei
    Copy the code