How to create an interface in Swift

前端 未结 2 1942
予麋鹿
予麋鹿 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条回答
  •  佛祖请我去吃肉
    2021-02-13 11:36

    Instead of interface swift have Protocols.

    A protocol defines a blueprint of methods, properties, and other requirements that suit a particular task or piece of functionality. The protocol can then be adopted by a class, structure, or enumeration to provide an actual implementation of those requirements. Any type that satisfies the requirements of a protocol is said to conform to that protocol.

    lets take an exam .

    protocol Animal {
        func canSwim() -> Bool
    }
    

    and we have a class that confirm this protocol name Animal

    class Human : Animal {
       func canSwim() -> Bool {
         return true
       }
    }
    

    for more go to - https://developer.apple.com/library/content/documentation/Swift/Conceptual/Swift_Programming_Language/Protocols.html

提交回复
热议问题