是否有可能Objective-C
类来实现Swift
协议中的.h
以便其他Swift
类可以参考Objective-C
类由Swift
协议类型?
Let MySwiftProtocol
be the Swift
协议类型,现在假设我有这个.h
对于我的班级:
@protocol MySwiftProtocol;
@interface MyObj : NSObject< MySwiftProtocol>
然后我会收到警告:cannot find protocol definition for MySwiftProtocol
警告没有什么好处,所以这不起作用。
所以我们假设我在.m
并删除对协议的引用.h
.
@interface MyObj () <MySwiftProtocol>
然后我最终无法投射类型的对象MyObj
输入MySwiftProtocol
in Swift
类。
所以上面的方法都行不通。
请帮忙!
据苹果称。Forward declarations of Swift classes and protocols can only be used as types for method and property declarations
。苹果进一步表示,An Objective-C class can adopt a Swift protocol in its implementation (.m) file by importing the Xcode generated header for Swift code...
基本上你不能将 obj-c 类标记为符合 .h 中的 swift 协议。因此,您必须在 .m 中执行此操作,然后让该类将其自身传递给任何需要引用 objc 对象作为 swift 协议类型的对象。
参见章节Referencing a Swift Class or Protocol in an Objective-C Header
and Adopting a Swift Protocol in an Objective-C Implementation
at https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html https://developer.apple.com/library/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html
本文内容由网友自发贡献,版权归原作者所有,本站不承担相应法律责任。如您发现有涉嫌抄袭侵权的内容,请联系:hwhale#tublm.com(使用前将#替换为@)