【问题标题】:Swift Metatyping InconsistencySwift 元类型不一致
【发布时间】:2016-02-03 18:34:30
【问题描述】:

给定以下代码...

protocol MyProtocol {}
enum MyEnum: MyProtocol {}
struct MyStruct: MyProtocol {}
class MyClass: MyProtocol {}
func MyFunction(parameter: MyProtocol.Type) -> String {
    return "Hi"
}

为什么我在分配变量时必须使用.self...

var variable: MyProtocol.Type = MyStruct.self

而不是当我将元类型作为函数参数传递时?

var result = MyFunction(MyStruct)

【问题讨论】:

标签: ios swift introspection metatype


【解决方案1】:

它看起来像一个编译器错误。我个人觉得 .self 应该包含在函数参数中。

不过,一旦 Swift 的源代码在今年晚些时候发布,可能会有更好的解释。同时,您可以提交雷达。

编辑:如果Class.Type 是唯一的函数参数,它似乎放弃了.self 要求。 Swift leave out .self to invoke a function which needs metatype?

【讨论】:

    猜你喜欢
    • 2021-10-13
    • 2016-05-04
    • 1970-01-01
    • 2015-01-26
    • 2013-03-25
    • 1970-01-01
    • 1970-01-01
    • 2014-10-18
    • 1970-01-01
    相关资源
    最近更新 更多