Whipper Snapper
Whipper Snapper

Reputation: 255

Why can't I use my swift files in my objective-c code?

I have read multiple tutorials on how to use swift classes in objective-c classes, and it seems straightforward enough, but I still can't get it to work.

I have a test class for swift called UserDefaultsFactory.swift containing this:

class UserDefaultFactory {
    func a() {
        print("šŸ¹HELLO")
    }
}

And in my objective-c class I have imported the project name-swift like this: #import "NetPBXMobile-Swift.h". This works without any errors, and if I access the file I can see that it's full with references to swift classes. But for some reason it doesn't contain my own class UserDefaultsFactory.

I have cleaned and build the code, but still nothing. As far as I understand the NetPBXMobile-Swift.h is created automatically, as I can see that it contains my own swift functions from other classes I have previously created.

I have tried initiating an object like this:

UserDefaultFactory a = [[UserDefaultFactory alloc] init];

But I get the error message that it's an undeclared identifier.

Im' I using the right approach or is there another way to do this?

Upvotes: 1

Views: 438

Answers (4)

DonMag
DonMag

Reputation: 77690

To clarify... Your class must be marked @objc and must derive from NSObject, and your func must also be marked @objc:

@objc class UserDefaultFactory: NSObject {
    @objc func a() {
        print("šŸ¹HELLO")
    }
}

To then call that func from Objective-C code:

UserDefaultFactory *udf = [UserDefaultFactory new];
[udf a];

You could also call it via:

[[UserDefaultFactory new] a];

but more than likely you will be creating an instance of your UserDefaultFactory object for additional usage.

Upvotes: 2

Scriptable
Scriptable

Reputation: 19758

You need to mark the class as available to Objective-c by prefixing the class with @objc

try this

@objc class UserDefaultFactory: NSObject {
    func a() {
        print("šŸ¹HELLO")
    }
} 

Upvotes: 2

ATV
ATV

Reputation: 367

Make sure your Swift class inherits from a class that derives (directly or indirectly) from NSObject.

Marking your Swift class as @objc without any inheritance described above - you will get error "Only classes that inherit from NSObject can be declared @objc"

Marking your Swift class as @objcMembers without any inheritance - you will get error in Obj-c part "Unknown type name"

Upvotes: 1

siddhant
siddhant

Reputation: 39

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. A dialogue box will appear, make sure to select ā€œCreate Bridging Headerā€ when prompted. The first time you add a swift file to your project is the only time this prompt will appear. Selecting Create Bridge Header right away will save you the trouble of creating it manually.

  1. Next, after selecting ā€œCreate Bridging Headerā€ or creating it manually, go to your projectā€™s general settings. Select the proper target for your app. Go to ā€œBuild Settingsā€ and switch to ā€œAllā€, instead of ā€œBasicā€ which is the default. Here search for the ā€œPackagingā€ section. Turn on ā€œDefines Moduleā€, by changing ā€œNoā€ to ā€œYesā€.

Reference: https://medium.com/ios-os-x-development/swift-and-objective-c-interoperability-2add8e6d6887

Upvotes: 1

Related Questions