How to Use Objective-C Source Files in a Swift Project

Asked 2 years ago, Updated 2 years ago, 65 views

From now on, I would like to develop iOS applications with Swift, but I would also like to utilize the source code of Objective-C that I have created so far.How do I configure the Objective-C source code to be included in the Swift project?

xcode swift objective-c

2022-09-30 19:15

2 Answers

<ProductName>-Bridging-Header.h must be created so that Swift can reference the Objective-C class.

Below is the procedure for using the Objective-C class for the new Swift project.

Create Any Class with Objective-C

#import<Foundation/Foundation.h>

@ interfaceObjcClass —NSObject
-(void)log;
@end
@implementationObjcClass
-(void)log{
    NSLog (@"Objc Class Log");
}
@end

Select YES as you are asked if you want to create Bridging-Header.h

Import the Objective-C class you want to use from Swift into -Bridging-Header.h

#import "ObjcClass.h"

Write the code using the Objective-C class from the swift file

func application(application:UIApplication, didFinishLaunchingWithOptions launchOptions:[NSObject:AnyObject]?) ->Bool{
    varobjcClass=ObjcClass()
    objcClass.log()
    // Override point for customization after application launch.
    return true
}

I will also post a sample project.


2022-09-30 19:15

You must first create a Bridging Header file that bridges between Objective-C and Swift.

XCode 6.1 Japanese Language Planning Table of Contents is familiar.

Other precautions for interoperability are summarized and recommended.


2022-09-30 19:15

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.