我有一个Objective-C方法接受id类型的参数,我想传递一个
Swift结构.
@implementation Objcclass + (void)addListener:(id)listener { // Do something with listener }
DemoStruct.swift文件:
struct DemoStruct { func registerasListener() { Objcclass.addListener(self) // Can't find a way to do this } }
我得到的编译错误消息:
Type ‘DemoStruct’ does not conform to protocol ‘AnyObject’
所以我的问题是,如何使Objective-C方法接受Any而不是AnyObject并且有这样的事情?
你不能这样做.
从Objective-C无法访问Swift结构.这在Apple的“使用Swift With Cocoa和Objective-C”一书中有所说明:
You’ll have access to anything within a class or protocol that’s marked with the @objc attribute as long as it’s compatible with Objective-C. This excludes Swift-only features such as those listed here:
- Generics
- Tuples
- Enumerations defined in Swift
- Structures defined in Swift
- Top-level functions defined in Swift
- Global variables defined in Swift
- Typealiases defined in Swift
- Swift-style variadics
- nested types
- Curried functions
摘录自:Apple Inc.“将Swift与Cocoa和Objective-C结合使用.”iBooks. https://itun.es/gb/1u3-0.l
原文地址:https://www.jb51.cc/swift/319563.html
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。