swift - Swift Generic struct 符合与 associatedType 的协议得到“无法将'String'类型的返回表达式转换为返回类型'Key'”错误
问题描述
protocol Container {
associatedtype ItemType
func getItem() -> ItemType
func getItems() -> [ItemType]
}
struct TypedBackpack<TypedItemType: StringProtocol>: Container {
func getItem() -> TypedItemType {
return ""
}
func getItems() -> [TypedItemType] {
return [""]
}
}
struct KeyBackpack<Key: Sequence>: Container {
func getItem() -> Key { //"Error: Cannot convert return expression of type 'String' to return type 'Key'"
return ""
}
func getItems() -> [Key] { //"Error: Cannot convert return expression of type 'String' to return type 'Key'"
return [""]
}
}
对于 KeyBackPack 中的“getItem()”和“getItems()”函数,我得到“错误:无法将“String”类型的返回表达式转换为“Key”类型的返回表达式。但是,TypedBackPack 工作正常。
如果我将 KeyBackPack 更改为:
struct KeyBackpack<Key: StringProtocol>: Container {
func getItem() -> Key {
return ""
}
func getItems() -> [Key] {
return [""]
}
}
一切正常。所以问题是“<Key: Sequence>”。谁能告诉我为什么 <Key: StringProtocol> 工作正常但 <Key: Sequence> 不行?
解决方案
根据您的声明,我可以使用 anySequence
作为 的类型参数KeyBackpack
,getItem
并将返回该类型的实例。
所以理论上,我可以这样做:
let intArray = KeyBackpack<[Int]>().getItem()
[Int]
如果明显 a Sequence
,getItem
应该给我一个[Int]
。但是,getItem
实际返回的是一个空字符串!显然这没有意义!
那为什么会Key: StringProtocol
起作用?
这是因为StringProtocol
继承自ExpressibleByStringInterpolation
,而后者又继承自ExpressibleByStringLiteral
。
这意味着您可以将字符串文字隐式转换为Key
, 因为Key
符合ExpressibleByStringLiteral
.
更具体地说,从StringProtocol
继承需求,编译器会自动转换为对该初始化程序的调用。您可以将其视为:init(stringLiteral:)
ExpressibleByStringLiteral
return ""
return Key(stringLiteral: "")
这也意味着,如果您返回字符串文字以外的内容,Key: StringProtocol
则将无法正常工作。例如
let s = "" // here the type of "s" is inferred to be String
return s // "cannot convert type..." error because "s" is not a string literal, nor of type "Key"
推荐阅读
- html - 如何在 Css 类中设置优先级?
- javascript - 在 jquery 数据表中选择单个复选框
- javascript - 验证反应js中的输入
- python - 来自私人 github 存储库的 pip 安装包,在 docker 中使用部署密钥
- glsl - glsl片段着色器颜色添加逻辑
- android - Android - 如何从 Firebase 解锁您的 IP 地址?
- angular - 从 Angular 发送 Firebase 推送通知时出错
- r - 冲积地块内部公司运动
- flutter - 有没有办法避免在 Flutter 的 Navigator _history 中出现重复?
- django - 从日期时间范围内的 Django 模型中获取数据