Using Swift class inside Objective-C

I am getting an error file not found

while trying #import "DodgeHeroReal-Swift.h

. The name of my product module is set to DodgeHeroReal

, and the Modules option is also set to YES

(both target and project level for both build settings). I already have a Bridging header that Xcode generated. I have a quick emojisClass that I want to use in my Objective-C ViewController EmojiShopViewController.

I have looked through many great StackOverflow posts regarding mixed objc and swift projects, but none of the suggested solutions seem to work. I also looked at Apple's documentation page, but that doesn't help either. Can anyone walk me through how I can use the following swift class in my ViewController.m?

HereGlobalFunc.swift

(which has a class emojisClass

)

@objc(EmojiClass) class emojisClass {
    @objc(buyAngle) func buyAngle(){
        if(emotiBucks >= 10000){
             var value = emotiBucks-10000
             NSUserDefaults.standardUserDefaults().setBool(true, forKey: "haveAngleEmoji")
            NSUserDefaults.standardUserDefaults().setInteger(value, forKey: "emotiBucks")
            NSUserDefaults.standardUserDefaults().synchronize()
        }
    }
    func buyAlien(){
        if(emotiBucks >= 2000){
            var value = emotiBucks-2000
            NSUserDefaults.standardUserDefaults().setBool(true, forKey: "haveAlienEmoji")
            NSUserDefaults.standardUserDefaults().setInteger(value, forKey: "emotiBucks")
            NSUserDefaults.standardUserDefaults().synchronize()
        }
    }
    func buySassy(){
        if(emotiBucks >= 5000){
            var value = emotiBucks-5000
            NSUserDefaults.standardUserDefaults().setBool(true, forKey: "haveSassyEmoji")
            NSUserDefaults.standardUserDefaults().setInteger(value, forKey: "emotiBucks")
            NSUserDefaults.standardUserDefaults().synchronize()
        }
    }
    func buyHeart(){
        if(emotiBucks >= 3000){
            var value = emotiBucks-3000
            NSUserDefaults.standardUserDefaults().setBool(true, forKey: "haveHeartEmoji")
            NSUserDefaults.standardUserDefaults().setInteger(value, forKey: "emotiBucks")
            NSUserDefaults.standardUserDefaults().synchronize()
        }
    }   
}

      

I imported the header of the bridge in its EmojiShopviewController.m: #import "Dodge Hero-Bridging-Header.h"

. Now I want to be able to call the buyAngle () function that was declared in my swift file.

EDIT . Initially the project was quick, then I added the Obj-C files and then the Bridging-Header. This way the project contains .swift and objc files and now I want to be able to generate ProductModule-Swift.h

without deleting any of my current files.

EDIT 2 : Run find * -iname '*Swift.h'

and then CMD + F in terminal in my DerivedData directory will return this:

****** Other stuff ********
DodgeHeroReal-gmmvxsfrydixythkdwqjpncbqlpl/Build/Intermediates/DodgeHeroReal.build/Debug-iphonesimulator/DodgeHeroRealTests.build/DerivedSources/DodgeHeroReal-Swift.h
DodgeHeroReal-gmmvxsfrydixythkdwqjpncbqlpl/Build/Intermediates/DodgeHeroReal.build/Debug-iphonesimulator/DodgeHeroRealTests.build/DerivedSources/DodgeHeroRealTests-Swift.h
DodgeHeroReal-gmmvxsfrydixythkdwqjpncbqlpl/Build/Intermediates/DodgeHeroReal.build/Debug-iphonesimulator/DodgeHeroRealTests.build/Objects-normal/x86_64/DodgeHeroReal-Swift.h
****** More other stuff ********

      

+3


source to share


2 answers


I found that sometimes an error in the swift file makes the ProjectName-Swift.h file not generated and there is no warning. Try to get all the fast files without errors, even if you have to tamper with them temporarily, for example by commenting something or adding dummy code that compiles without errors. Also, make sure the swift classes you refer to in Objective-C inherit from NSObject or some other Objective-C class, and make sure they are not all private. I spent days trying to figure out why this error happened - it can be very frustrating!



Keep in mind that you can click on the file name of the Swift → Objective-C bridge file in the import statement and Xcode will show you its contents. If you get an error beep instead, it seems to mean the file was not found, so it probably wasn't generated.

+2


source


I ran into this once and my problem was an enum in the same rushing file that was not tagged with @objc.



+1


source







All Articles