简体   繁体   English

在Objective-C中使用Swift类

[英]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. 当我尝试#import "DodgeHeroReal-Swift.h时,我收到file not found错误。我的产品模块名称设置为DodgeHeroReal ,定义模块也设置为YES (两者都在目标级别和项目级别进行两次构建设置我已经有了一个由Xcode生成的Bridging标头。我有一个swift类emojisClass,我希望能够在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. 我看过很多关于混合objc和swift项目的精彩StackOverflow帖子,但是所提出的解决方案似乎都没有用。 I've looked at the Apple Documentation page too but that doesn't help either. 我也查看了Apple Documentation页面,但这也没有帮助。 Can someone walk me through how I can use the following swift class in my ViewController.m? 有人可以告诉我如何在ViewController.m中使用以下swift类吗?

Here is GlobalFunc.swift (which has the class emojisClass ) 这是GlobalFunc.swift (它有类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" . 我在我的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. 现在我希望能够调用我的swift文件中声明的函数buyAngle()。

EDIT : The project was originally a swift project, then I added Obj-C files, then the Bridging-Header. 编辑 :该项目最初是一个快速的项目,然后我添加了Obj-C文件,然后是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. 所以该项目包含.swift和objc文件,现在我希望能够生成ProductModule-Swift.h而不删除任何当前文件。

EDIT 2 : running find * -iname '*Swift.h' and then a CMD+F in Terminal in my DerivedData directory returns this: 编辑2 :运行find * -iname '*Swift.h'然后在我的DerivedData目录中的终端中的CMD + F返回:

****** 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. 我发现有时swift文件中的错误会导致ProjectName-Swift.h文件无法生成,并且没有警告。 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. 尝试让所有swift文件都没有错误,即使你必须暂时伪造它,例如,通过注释某些东西或添加编译而没有错误的虚拟代码。 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. 另外,请确保您从Objective-C引用的swift类继承自NSObject或其他一些Objective-C类,并确保它们不是全部标记为私有。 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. 请注意,您可以在import语句中按命令单击Swift-> Objective-C桥接文件的文件名,Xcode将显示其内容。 If it gives an error beep instead, that apparently means the file was not found so it probably wasn't generated. 如果它发出错误蜂鸣声,那显然意味着找不到文件,因此可能没有生成。

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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