1

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?

2
  • Did any of the comments below help you? Commented Oct 4, 2019 at 14:34
  • @ATV Yes, I finally got it to work. Thanks for all the support! sorry for the late response. Commented Oct 5, 2019 at 10:17

4 Answers 4

2

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.

Sign up to request clarification or add additional context in comments.

1 Comment

That worked wonderfully! Thank you so much for your help and for taking the time! :)
2

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")
    }
} 

1 Comment

Thanks but I got the error Only classes that inherit from NSObject can be declared @objc
1

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

1 Comment

Thanks, but still get the same error even after clean and build. What exactly is "Defines Module" by the way?
1

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"

2 Comments

Could you elaborate? I don't fully understand "Make sure your Swift class inherits from a class that derives (directly or indirectly) from NSObject."
@WhipperSnapper in simple words you cannot use Swift features that are not defined in Obj-C (e.g. classes with no inheritance, Swift generics, enums without Int types, etc). So regarding the documentation Obj-C class should has a root class. You can just define your class as class UserDefaultFactory: NSObject or use any needed child of NSObject.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.