简体   繁体   English

协议中的类型变量-Swift 2

[英]Type variable in protocol - Swift 2

So I have a protocol, and in it I want a variable that is a class type. 因此,我有一个协议,并且在其中需要一个类类型的变量。 That way I can init that class from the variable. 这样,我可以从变量中初始化该类。

Keep in mind that there will be many different classes. 请记住,会有许多不同的类。 I made a quick example. 我举了一个简单的例子。

I get the error "type 'CashRegister' does not conform to protocol 'RegisterProtocol'" 我收到错误“类型'CashRegister'不符合协议'RegisterProtocol'”

This example isn't exactly what I'm doing, but it gets the point across. 这个例子并不完全是我在做的事情,但可以理解这一点。 Thanks for the help. 谢谢您的帮助。

protocol RegisterProtocol {
    var currentBill: DollarBillProtocol {get set}
    func makeNewBill()->DollarBillProtocol
}

extension RegisterProtocol {
    func printCurrentBill() {
        Swift.print(currentBill)
    }
}

class CashRegister: RegisterProtocol {

    var currentBill = OneDollarBill.self

    func makeNewBill() -> DollarBillProtocol {
        return currentBill.init()
    }
}



protocol DollarBillProtocol {
    // protocol that all bills have in common
}


class OneDollarBill: DollarBillProtocol {
    required init(){
    }
}

class FiveDollarBill: DollarBillProtocol {
    required init(){
    }

}

The way you declare currentBill in CashRegister makes it a var of type class . CashRegister声明currentBill的方式使其成为class类型的var。 But the protocol RegisterProtocol requires this variable to be of type DollarBillProtocol in any class that implements the protocol. 但是协议RegisterProtocol要求该变量在实现该协议的任何类中的类型都为DollarBillProtocol The compile error is because of this mismatch. 编译错误是由于此不匹配。

To make this more clear, you could declare the var with the explicit type, as follows: 为了使这一点更加清楚,您可以使用显式类型声明var,如下所示:

class CashRegister: RegisterProtocol {

    var currentBill: DollarBillProtocol = OneDollarBill() // or other initial value
}

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 协议作为快速冲突中的参数类型 - protocol as parameter type in swift conflicts swift5 - 枚举协议 - 自我和自我以及一个变量 - swift5 - enum protocol - Self and self and a variable 类型'ViewController'不符合协议'UIDocumentPickerDelegate'swift 2 - Type 'ViewController' does not conform to protocol 'UIDocumentPickerDelegate' swift 2 Swift-类型'MenuViewController'不符合协议'GKGameCenterControllerDelegate' - Swift - Type 'MenuViewController' does not conform to protocol 'GKGameCenterControllerDelegate' Swift:如何将类型转换为特定协议? - Swift: how to type cast a protocol into a specific one? Swift:类型'ViewController'不符合协议'UIPageViewControllerDataSource' - Swift: Type 'ViewController' does not conform to protocol 'UIPageViewControllerDataSource' Swift:`类型'[String]'不符合协议'StringLiteralConvertible' - Swift: `Type '[String]' does not conform to protocol 'StringLiteralConvertible'` 类型“myViewController”不符合Swift中的协议UIPIckerDataSource - Type “myViewController” does not conform to protocol UIPIckerDataSource in Swift 如何使用泛型协议作为变量类型 - How to use generic protocol as a variable type 如何快速创建通用关联类型协议的数组? - How to create array of generic associated type protocol in swift?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM