美文网首页
Swift中associatedtype

Swift中associatedtype

作者: xmb | 来源:发表于2021-01-26 14:10 被阅读0次
    // associatedtype定义关联类型,相当于类型的占位符,让实现协议的类型来指定具体的类型
    
    protocol Food {
        
    }
    
    protocol Animal {
        associatedtype F: Food
        func eat(_ food: F)
    }
    
    struct Meat: Food {
        
    }
    
    struct Grass: Food {
        
    }
    
    struct Tiger: Animal {
        func eat(_ food: Meat) {
            print("eat \(food)")
        }
    }
    
    struct Sheep: Animal {
        func eat(_ food: Grass) {
            print("eat \(food)")
        }
    }
    
    func test() {
        let tiger = Tiger()
        tiger.eat(Meat())
    
        let sheep = Sheep()
        sheep.eat(Grass())
    }
    
    /*
    
    Protocol 'Animal' can only be used as a generic constraint because it has Self or associated type requirements
    只能被用作泛型约束,不能当做独立的类型使用
     
    错误代码:
    func isTiger(animal: Animal) -> Bool {
    
    }
    */
    
    // 具有关联类型的协议类型,只能当做泛型约束使用
    func isTiger<A: Animal>(animal: A) -> Bool {
        if animal is Tiger {
            return true
        } else {
            return false
        }
    }
    
    

    相关文章

      网友评论

          本文标题:Swift中associatedtype

          本文链接:https://www.haomeiwen.com/subject/qsugzktx.html