Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Inherit from a Swift class in Objective C

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.

Here's what i understand:

-- To declare an Obj-C class as inheriting from something, that must be in the .h file after the ':':

#import <UIKit/UIKit.h> @interface RootViewController : UIViewController <UITableViewDataSource, UITableViewDelegate> @end 

-- To make Swift classes visible, that is #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?

like image 430
BaseZen Avatar asked Feb 06 '16 18:02

BaseZen


People also ask

Can Objective-C class inherit from Swift class?

Unfortunately, it's not possible to subclass a Swift class in Objective-C. Straight from the docs: You cannot subclass a Swift class in Objective-C.

Can we use Swift class in Objective-C?

You can work with types declared in Swift from within the Objective-C code in your project by importing an Xcode-generated header file. This file is an Objective-C header that declares the Swift interfaces in your target, and you can think of it as an umbrella header for your Swift code.

How do I access a Swift file in Objective-C?

To access and use swift classes or libraries in objective-c files start with an objective-c project that already contains some files. Add a new Swift file to the project. In the menu select File>New>File… then select Swift File, instead of Cocoa Touch Class. Name the file and hit create.

Can I use Swift library in Objective-C?

The Swift library cannot be directly called from Objective-C, since it is missing the required annotations in the code, and in many cases, modules do not inherit from NSObject, rather they use the native Swift data types.


2 Answers

Unfortunately, it's not possible to subclass a Swift class in Objective-C. Straight from the docs:

You cannot subclass a Swift class in Objective-C.

See Apple's guide on interoperability for more details on what you can and cannot access with Objective-C.

like image 197
Edward Jiang Avatar answered Sep 22 '22 00:09

Edward Jiang


As for Xcode 8.0 and earlier there is dirty-hacky solution, that probably will be fixed in the future.

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 class.

import Foundation  class SwiftClass : NSObject {   func say() {     print("hi");   } } 

Objc class:

#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.

like image 40
Alfred Zien Avatar answered Sep 22 '22 00:09

Alfred Zien