I'm successfully mixing and matching Obj-C and Swift in an Xcode 7 project. However, I can't seem to figure out how, in an Objective C class, to inherit from a Swift class (and yes I know about declaring that Swift class as @objc
for visibility). In this case the desired Swift superclass MySwiftViewController
is a subclass of UIViewController
. For now, in Obj-C, I'm inheriting directly from UIViewController
and not gaining access to the capabilities I added in MySwiftViewController
.
我在Xcode 7项目中成功地混合和匹配Obj-C和Swift。但是,我似乎无法弄清楚在Objective C类中如何从Swift类继承(是的,我知道将Swift类声明为@objc以获得可见性)。在这种情况下,所需的Swift超类MySwiftViewController是UIViewController的子类。现在,在Obj-C中,我直接从UIViewController继承并且无法访问我在MySwiftViewController中添加的功能。
Here's what i understand:
这是我的理解:
-- To declare an Obj-C class as inheriting from something, that must be in the .h file after the ':':
- 要将Obj-C类声明为继承某些东西,它必须位于':'之后的.h文件中:
#import <UIKit/UIKit.h>
@interface RootViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
@end
-- To make Swift classes visible, that is #import
ed:
- 要使Swift类可见,那就是#imported:
#import "MyProject-Swift.h"
However, you cannot import the Swift auto-generated bridging header into the Obj-C .h file. You also cannot forward-declare an opaque superclass with @class
. So, is this possible and how?
但是,您无法将Swift自动生成的桥接头导入Obj-C .h文件。您也无法使用@class正向声明不透明的超类。那么,这可能吗?如何?
2 个解决方案
#1
25
Unfortunately, it's not possible to subclass a Swift class in Objective-C. Straight from the docs:
不幸的是,不可能在Objective-C中继承Swift类。直接来自文档:
You cannot subclass a Swift class in Objective-C.
您不能在Objective-C中继承Swift类。
See Apple's guide on interoperability for more details on what you can and cannot access with Objective-C.
有关使用Objective-C可以访问和不能访问的内容的更多详细信息,请参阅Apple的互操作性指南。
#2
3
As for Xcode 8.0 and earlier there is dirty-hacky solution, that probably will be fixed in the future.
对于Xcode 8.0和更早版本,有一个肮脏的hacky解决方案,可能将来会修复。
If you want to subclass from swift file, you can add objc_subclassing_restricted
attribute. You can make it as macro for convenience. Code:
如果要从swift文件中继承子类,可以添加objc_subclassing_restricted属性。为方便起见,您可以将其设为宏。码:
Swift class.
斯威夫特班。
import Foundation
class SwiftClass : NSObject {
func say() {
print("hi");
}
}
Objc class:
Objc类:
#import <Foundation/Foundation.h>
#import "test-Swift.h"
#define SWIFT_SUBCLASS __attribute__((objc_subclassing_restricted))
SWIFT_SUBCLASS
@interface ObjcClass : SwiftClass
- (instancetype)init;
@end
@implementation ObjcClass
- (void)say {
NSLog(@"oops");
}
@end
But, as I understand, it is not supported, and you may have any sort of bugs because of it. So it is not guide to action, and more like curious thing to know.
但是,据我所知,它不受支持,因此你可能会遇到任何类型的错误。所以它不是行动的指南,更像是好奇的事情要知道。
#1
25
Unfortunately, it's not possible to subclass a Swift class in Objective-C. Straight from the docs:
不幸的是,不可能在Objective-C中继承Swift类。直接来自文档:
You cannot subclass a Swift class in Objective-C.
您不能在Objective-C中继承Swift类。
See Apple's guide on interoperability for more details on what you can and cannot access with Objective-C.
有关使用Objective-C可以访问和不能访问的内容的更多详细信息,请参阅Apple的互操作性指南。
#2
3
As for Xcode 8.0 and earlier there is dirty-hacky solution, that probably will be fixed in the future.
对于Xcode 8.0和更早版本,有一个肮脏的hacky解决方案,可能将来会修复。
If you want to subclass from swift file, you can add objc_subclassing_restricted
attribute. You can make it as macro for convenience. Code:
如果要从swift文件中继承子类,可以添加objc_subclassing_restricted属性。为方便起见,您可以将其设为宏。码:
Swift class.
斯威夫特班。
import Foundation
class SwiftClass : NSObject {
func say() {
print("hi");
}
}
Objc class:
Objc类:
#import <Foundation/Foundation.h>
#import "test-Swift.h"
#define SWIFT_SUBCLASS __attribute__((objc_subclassing_restricted))
SWIFT_SUBCLASS
@interface ObjcClass : SwiftClass
- (instancetype)init;
@end
@implementation ObjcClass
- (void)say {
NSLog(@"oops");
}
@end
But, as I understand, it is not supported, and you may have any sort of bugs because of it. So it is not guide to action, and more like curious thing to know.
但是,据我所知,它不受支持,因此你可能会遇到任何类型的错误。所以它不是行动的指南,更像是好奇的事情要知道。