Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/17.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Swift 如何将协议仅限于值类型?_Swift_Swift2 - Fatal编程技术网

Swift 如何将协议仅限于值类型?

Swift 如何将协议仅限于值类型?,swift,swift2,Swift,Swift2,在精神上类似于,除了,我希望能够定义一个只能由枚举、结构等采用的协议,而不是将协议仅限于类。。这可能吗?我找不到答案,但通过反复研究,我得出结论,你不能。由于要将协议仅限于类,所以在它前面加上classlike protocol SomeProto: class { func structYourStuff() -> Void } 假设这在其他类型中是一致的,我尝试了 protocol SomeProto: struct { func structYourStuff()

在精神上类似于,除了,我希望能够定义一个只能由枚举、结构等采用的协议,而不是将协议仅限于类。。这可能吗?

我找不到答案,但通过反复研究,我得出结论,你不能。由于要将协议仅限于类,所以在它前面加上
class
like

protocol SomeProto: class {
    func structYourStuff() -> Void
}
假设这在其他类型中是一致的,我尝试了

protocol SomeProto: struct {
    func structYourStuff() -> Void
}

但是Xcode在一行中给了我五个不同的错误,这让我得出结论,你不能。我可能完全错了,大约一周前我才开始学习Swift,确切地说,我们不能这样做。嗯,这将是语言的一个有用的扩展。@CouchDeveloper你让我想到了,也许有一些方法可以构建封闭的功能,因为我们有自定义操作符的灵活性。但我想不出这是怎么回事。