如何使用@objc 标签传递 swift 枚举 [英] How to pass swift enum with @objc tag

查看:25
本文介绍了如何使用@objc 标签传递 swift 枚举的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要定义一个可以在使用某些 Objective-c 类型的类中调用的协议

I need to define a protocol which can be called in a class that use some Objective-c type

但是这样做是行不通的:

But doing that doesn't work:

enum NewsCellActionType: Int {
    case Vote = 0
    case Comments
    case Time
}

@objc protocol NewsCellDelegate {
    func newsCellDidSelectButton(cell: NewsCell, actionType: NewsCellActionType)
}

你得到他的错误

Swift enums cannot be represented in Objective-C

如果我不将@objc 标记放在我的协议上,它会在应用程序在采用协​​议并从Objective-C 类型类(如UIViewController)继承的类中调用时立即崩溃.

If I don't put the @objc tag on my protocol it'll crash the app as soon as it's called in a class which adopt the protocol AND inherit from an Objective-C type class (like a UIViewController).

所以我的问题是,我应该如何使用@objc 标签声明和传递我的枚举?

So my question is, how should I declare and pass my enum with the @objc tag?

推荐答案

Swift 枚举与 Obj-C(或 C)枚举非常不同,它们不能直接传递给 Obj-C.

Swift enums are very different from Obj-C (or C) enums and they can't be passed directly to Obj-C.

作为一种解决方法,您可以使用 Int 参数声明您的方法.

As a workaround, you can declare your method with an Int parameter.

func newsCellDidSelectButton(cell: NewsCell, actionType: Int)

并将其作为 NewsCellActionType.Vote.toRaw() 传递.但是,您将无法从 Obj-C 访问枚举名称,这会使代码变得更加困难.

and pass it as NewsCellActionType.Vote.toRaw(). You won't be able to access the enum names from Obj-C though and it makes the code much more difficult.

更好的解决方案可能是在 Obj-C 中实现枚举(例如,在你的新娘头文件中),因为这样它就会在 Swift 中自动访问,并且可以将它作为参数传递.

A better solution might be to implement the enum in Obj-C (for example, in your briding header) because then it will be automatically accessible in Swift and it will be possible to pass it as a parameter.

编辑

不需要添加 @objc 只是为了将它用于 Obj-C 类.如果您的代码是纯 Swift,则可以毫无问题地使用枚举,请参阅以下示例作为证明:

It is not required to add @objc simply to use it for an Obj-C class. If your code is pure Swift, you can use enums without problems, see the following example as a proof:

enum NewsCellActionType : Int {
    case Vote = 0
    case Comments
    case Time
}

protocol NewsCellDelegate {
    func newsCellDidSelectButton(cell: UITableViewCell?, actionType: NewsCellActionType    )
}

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate, NewsCellDelegate {

    var window: UIWindow?

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: NSDictionary?) -> Bool {
        self.window = UIWindow(frame: UIScreen.mainScreen().bounds)

        self.window!.backgroundColor = UIColor.whiteColor()
        self.window!.makeKeyAndVisible()

        test()

        return true;
    }

    func newsCellDidSelectButton(cell: UITableViewCell?, actionType: NewsCellActionType) {
        println(actionType.toRaw());
    }

    func test() {
        self.newsCellDidSelectButton(nil, actionType: NewsCellActionType.Vote)
    }
}

这篇关于如何使用@objc 标签传递 swift 枚举的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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