简体   繁体   中英

Using Swift class inside Objective-C

I'm getting a file not found error when I try to #import "DodgeHeroReal-Swift.h . My Product Module name is set to DodgeHeroReal and Defines Modules is also set to YES (both at target level and project level for both build settings). I already have a Bridging header that was generated by Xcode. I have a swift class emojisClass that I want to be able to use in my Objective-C ViewController EmojiShopViewController.

I've looked at the many wonderful StackOverflow posts regarding mixed objc and swift projects, yet none of the solutions proposed seem to work. I've looked at the Apple Documentation page too but that doesn't help either. Can someone walk me through how I can use the following swift class in my ViewController.m?

Here is GlobalFunc.swift (which has the 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've imported the bridging header in my EmojiShopviewController.m: #import "Dodge Hero-Bridging-Header.h" . Now I want to be able to call the function buyAngle() that's declared in my swift file.

EDIT : The project was originally a swift project, then I added Obj-C files, then the Bridging-Header. So the project contains .swift and objc files, and now I want to be able to generate the ProductModule-Swift.h without deleting any of my current files.

EDIT 2 : running find * -iname '*Swift.h' and then a CMD+F in Terminal in my DerivedData directory returns 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 ********

I find that sometimes an error in a swift file makes the ProjectName-Swift.h file not be generated and there is no warning. Try getting all the swift files to be error-free, even if you have to fake it temporarily, eg, by commenting something out or adding dummy code that compiles without error. Also, be sure the swift classes that you reference from Objective-C inherit from NSObject, or some other Objective-C class, and be sure they aren't all marked private. I've wasted days trying to figure out why this error was occurring -- it can be really frustrating!

Be aware that you can command-click on the file name of the Swift->Objective-C bridging file in the import statement and Xcode will show you its contents. If it gives an error beep instead, that apparently means the file was not found so it probably wasn't generated.

我碰到了这一次,我的问题是在同一个swift文件中的枚举没有用@objc标记标记。

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM