How to create an interface in Swift

前端 未结 2 1934
予麋鹿
予麋鹿 2021-02-13 11:16

i want to create functionality like interface in swift, my goal is when i call another class suppose i\'m calling API and the response of that class i want to reflect in to my c

2条回答
  •  -上瘾入骨i
    2021-02-13 11:51

    What are you finding is `Protocol'. Interfaces are same as protocol in Swift.

    protocol Shape {
        func shapeName() -> String
    }
    
    class Circle: Shape {
        func shapeName() -> String {
            return "circle"
        }
    
    }
    
    class Triangle: Shape {
        func shapeName() -> String {
            return "triangle"
        }
    }
    

    class and struct both can implements the protocol.

提交回复
热议问题