协议:为什么一致性检查和可选要求需要@ObjC? [英] Protocols: Why is @ObjC required for conformance checking and optional requirements?

查看:127
本文介绍了协议:为什么一致性检查和可选要求需要@ObjC?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

Swift文档说明了关于协议的以下内容


只有使用@objc属性将协议标记为
时,才能检查协议一致性,如以上为HasArea协议。这个
属性表示协议应该暴露给Objective-C
代码,并在使用Swift和Cocoa和Objective-C中描述。如果您不与Objective-C进行互操作,即使
,如果您想要检查
协议一致性,则需要使用@objc属性标记
协议。

You can check for protocol conformance only if your protocol is marked with the @objc attribute, as seen for the HasArea protocol above. This attribute indicates that the protocol should be exposed to Objective-C code and is described in Using Swift with Cocoa and Objective-C. Even if you are not interoperating with Objective-C, you need to mark your protocols with the @objc attribute if you want to be able to check for protocol conformance.

还要注意,@objc协议只能由类使用,而不是
通过结构或枚举。如果您以
的顺序将协议标记为@objc,以检查是否符合规定,您将可以将该
协议应用于类类型。

Note also that @objc protocols can be adopted only by classes, and not by structures or enumerations. If you mark your protocol as @objc in order to check for conformance, you will be able to apply that protocol only to class types.


只有您的协议
标有可选协议要求, @objc属性。即使您不使用
与Objective-C进行互操作,如果要指定可选要求,则需要使用@objc
属性标记协议。

Optional protocol requirements can only be specified if your protocol is marked with the @objc attribute. Even if you are not interoperating with Objective-C, you need to mark your protocols with the @objc attribute if you want to specify optional requirements.

还要注意,@objc协议只能由类使用,而不是
通过结构或枚举。如果您以
的顺序将协议标记为@objc,以指定可选要求,则只能将
的协议应用于类类型。

Note also that @objc protocols can be adopted only by classes, and not by structures or enumerations. If you mark your protocol as @objc in order to specify optional requirements, you will only be able to apply that protocol to class types.






为什么不能使用纯Swift协议(非 - @objc )检查一致性。为什么他们不具备可选要求?有人猜测底层的语言设计原因吗?


Why cannot pure Swift protocols (non-@objc) be checked for conformance. Why don't they have optional requirements? Can anybody guess the underlying language design reason?

我预计在未来有一些未定(可能很遥远)的时间,苹果将慢慢重新实现和替换可可和 CocoaTouch ,这些程序库完全在Swift中编程。那时候,如果我们不要使用任何Obj-C相关的东西,我们应该避免在我们的代码中使用可选的协议要求和协议检查一致性?

I expect that at some undetermined (probably far away) time in the future Apple will slowly reimplement and replace Cocoa and CocoaTouch with libraries programmed purely in Swift. At that time, if we wan't to avoid using any Obj-C related stuff, should we avoid using optional protocol requirements and protocol checks conformance in our code?

如果所以,Swift的惯用方式是如何使用 @objc 来实现类似的模式? (例如,可选方法的代表)

If so, what's Swift's idiomatic way of achieving similar patterns without using @objc? (E.g., a delegate with optional methods.)

例如,这种简单的用例不能用非 @objc 协议(和可打印 DebugPrintable 可流式不是 - @objc

For example, this simple use case cannot be implemented with non-@objc protocols (and Printable, DebugPrintable and Streamable are non-@objc:

import UIKit

let firstName = "John"
let lastName = "Appleseed"
let age = 33
let height = 1.74

let values: [Any] = [firstName, lastName, age, height]
let stringifiedValues = [String]()

for value in values
{
    if let pritanbleValue = value as? Printable
    {
        stringifiedValues.append(value.description)
    }
    else if let debugPrintableValue = value as? DebugPrintable
    {
        stringifiedValues.append(value.debugDescription)
    }
    else if let streamableValue = value as? Streamable
    {
        var string = ""
        streamableValue.writeTo(&string)
        stringifiedValues.append(string)
    }
    // etc.    
    else 
    {
        stringifiedValues.append("[NoStringRepresentation]")
    }
}


推荐答案

苹果公司员工jckarter表示:苹果开发人员论坛的主题: p>

jckarter, an Apple employee, said the following on an Apple Developer Forums' thread:


这是Swift运行时的一个限制。我们打算在将来的版本中删除此限制。

This is a limitation of Swift's runtime. We intend to remove this restriction in a future release.

这篇关于协议:为什么一致性检查和可选要求需要@ObjC?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆