如何使用 ObjC class 的方法,其中包含 Swift 中的前向声明 Swift class?
How can I use method of an ObjC class which contains a forward declare Swift class in Swift?
//CoolSwiftClass.swift
@objc(MyCoolSwiftClass)
class CoolSwiftClass: NSObject {}
//MyObjCViewController.h
@class MyCoolSwiftClass;
@interface MyObjCViewController : UIViewController
- (instancetype)initWithMyCoolSwiftClass:(MyCoolSwiftClass *)myCoolSwiftClassInstance;
@end
//SadSwiftClass.swift
class SadSwiftClass: UIViewController {
override func loadView() {
super.loadView()
//This won't work
let myObjCViewController = MyObjCViewController(coolSwiftClass: coolSwiftClassInstance)
}
}
貌似Swift能找到前向声明MyCoolSwiftClass
,但查不出其实是CoolSwiftClass
.
而且我在 Swift and Objective-C in the Same Project 中没有找到任何可以帮助我的东西。
更新于2015-11-19 13:34 CST
只需上传一个 repo 到 GitHub:SO33775908
更新于 2015-11-19 13:47 CST
找出解决方法:
没有特殊的 ObjC 名称。结帐分支 No special ObjC name
更新于 2015-11-19 14:47 CST
另一种生成警告的解决方法:
我找到的最好方法是解决方法 1:
没有特殊的 ObjC 名称
如果您已经使用那个特殊的 ObjC 名称有一段时间了,您可以使用类似这样的名称:
//ClassName_Alias.h
#define MyCoolSwiftClass CoolSwiftClass
//CoolSwiftClass.swift
@objc
class CoolSwiftClass: NSObject {}
请查看示例存储库的问题更新 1。
//CoolSwiftClass.swift
@objc(MyCoolSwiftClass)
class CoolSwiftClass: NSObject {}
//MyObjCViewController.h
@class MyCoolSwiftClass;
@interface MyObjCViewController : UIViewController
- (instancetype)initWithMyCoolSwiftClass:(MyCoolSwiftClass *)myCoolSwiftClassInstance;
@end
//SadSwiftClass.swift
class SadSwiftClass: UIViewController {
override func loadView() {
super.loadView()
//This won't work
let myObjCViewController = MyObjCViewController(coolSwiftClass: coolSwiftClassInstance)
}
}
貌似Swift能找到前向声明MyCoolSwiftClass
,但查不出其实是CoolSwiftClass
.
而且我在 Swift and Objective-C in the Same Project 中没有找到任何可以帮助我的东西。
更新于2015-11-19 13:34 CST
只需上传一个 repo 到 GitHub:SO33775908
更新于 2015-11-19 13:47 CST
找出解决方法:
没有特殊的 ObjC 名称。结帐分支 No special ObjC name
更新于 2015-11-19 14:47 CST
另一种生成警告的解决方法:
我找到的最好方法是解决方法 1:
没有特殊的 ObjC 名称
如果您已经使用那个特殊的 ObjC 名称有一段时间了,您可以使用类似这样的名称:
//ClassName_Alias.h
#define MyCoolSwiftClass CoolSwiftClass
//CoolSwiftClass.swift
@objc
class CoolSwiftClass: NSObject {}
请查看示例存储库的问题更新 1。