gpt4 book ai didi

ios - 为什么直接引用协议(protocol)方法swift编译不通过?

转载 作者:可可西里 更新时间:2023-11-01 01:17:57 25 4
gpt4 key购买 nike

<分区>

给定以下结构:

struct TestStruct {
func test() {
print("Something")
}
}

像这样引用 test 函数:

let testFunc = TestStruct.test // (TestStruct) -> () -> Void

为了执行该功能,您必须执行以下操作:

testFunc(TestStruct())() // Prints "Something"

假设您使 TestStruct 符合协议(protocol):

protocol Test {
func test()
}

extension TestStruct: Test {}

现在尝试像这样通过协议(protocol)引用 test 函数是行不通的:

let testFunc = Test.func // Does not compile, no error message

testFunc 的类型不应该是 (Test) -> () -> Void 吗?为什么现在要编译?

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com