Difference between type method and type instance method, etc?

后端 未结 5 530
清歌不尽
清歌不尽 2020-12-23 15:04

Note: I\'ve read the apple documentation and studied a swift book.

  1. I\'m confused on the difference between a \"type instance method\" (if such exists, corre

5条回答
  •  天涯浪人
    2020-12-23 15:48

    whyceewhite - thank you so much! You have clarified something that I could just not grasp! For those of you who come to this page and are operating on Swift 3+, please see the code below if you want to put the code into practice and see static in operation.

    class Circle {
    
    static let PI = 3.14
    
    var radius: Double
    
    init(radius: Double) {
        self.radius = radius
    }
    
    // Returns the area of this circle
    func area() -> Double {
        return Double.pi * radius
    }
    
    // Ridiculous class method for demonstration purposes
    static func printTypeName() {
        print("Circle")
    }
    }
    

    Then start trying Circle.printTypeName or the examples shown above! Great stuff!

提交回复
热议问题