Require type and protocol for method parameter

后端 未结 4 918
猫巷女王i
猫巷女王i 2020-12-29 04:11

I am playing around with Swift and am stumbling over the following problem: given I have the predefined class Animal:

//Predefined classes
class         


        
相关标签:
4条回答
  • 2020-12-29 04:20

    <> is in Objective-C: conforms to protocol

    <> is in Swift for generics

    You can do more with the where keyword

    Use where after the type name to specify a list of requirements—for example, to require the type to implement a protocol, to require two types to be the same, or to require a class to have a particular superclass.

    Excerpt From: Apple Inc. “The Swift Programming Language.” iBooks.

    0 讨论(0)
  • 2020-12-29 04:33

    To me, this seems more an architecture problem:

    Nameable is strange as a protocol. Logically, every Animal has the ability to be named so Animal should always conform to Nameable

    It would be much simpler to allow for nil names when the animal is not named instead of having animals that can have a name and animals that can't.

    Then you can enforce names in Zoo simply by an assert.

    0 讨论(0)
  • 2020-12-29 04:41

    You can use a generic with a where clause with multiple conditions.

    func addAnimal<T: Animal where T: Nameable>(animal: T) { ... }
    

    Amendment: You should probably make the whole class this generic so you can type the array properly

    class Zoo<T: Animal where T: Nameable> {
        var animals : T[] = []
        func addAnimal(a: T) {
            ...
        }
    }
    
    0 讨论(0)
  • 2020-12-29 04:41

    Swift 3 version

    In Swift 3 the structure changed a bit. That´s why you will get a deprecation warning for the old structure. Here´s the new one:

    For functions the expected protocols are located after the parameters definition of the function. Example:

    func addAnimal<T: Animal>(animal: T) where T: Nameable
    

    For enums or classes the structure also changed

    enum ZooEnum<T: Animal> where T: Nameable 
    
    class Zoo<T: Animal> where T: Nameable 
    
    0 讨论(0)
提交回复
热议问题