简体   繁体   English

如何将File.swift导入ViewController.h并反之?

[英]How can I import a File.swift into ViewController.h and viceversa?

I tried to make a file .swift to change the color from the navigation bar in a but when I tried to import the File.swift in the ViewController it shows me an error. 我试图创建一个文件.swift来改变导航栏中的颜色,但是当我试图在ViewController中导入File.swift时它会显示一个错误。

------- code-------- -------代码--------

//Esta clase se creo para dar color a la NavViewController por medio de su valor en RGB
import UIKit

class NavViewController: UINavigationController {

    override func viewDidLoad() {

        //Se divide el valor RGB entre 255.0

        UINavigationBar.appearance().barTintColor = UIColor(red: 110/255.0, green: 192/255.0, blue: 238/255.0, alpha: 1.0)



        super.viewDidLoad()

        // Do any additional setup after loading the view.
    }

    override func didReceiveMemoryWarning() {
        super.didReceiveMemoryWarning()
        // Dispose of any resources that can be recreated.
    }


    /*
    // MARK: - Navigation

    // In a storyboard-based application, you will often want to do a little preparation before navigation
    override func prepareForSegue(segue: UIStoryboardSegue, sender: AnyObject?) {
        // Get the new view controller using segue.destinationViewController.
        // Pass the selected object to the new view controller.
    }
    */

error says : Expected ; 错误说:预期; after top level declaration 在顶级声明之后

Here is the official documents from Apple: https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html 以下是Apple的官方文档: https//developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html

Use Swift in Objective-C 在Objective-C中使用Swift

you need to rely on an Xcode-generated header file to expose those files to Objective-C. 您需要依赖Xcode生成的头文件将这些文件公开给Objective-C。 This automatically generated file is an Objective-C header that declares the Swift interfaces in your target. 这个自动生成的文件是一个Objective-C头,它声明了目标中的Swift接口。

  1. In the app target building settings , you could find your Product Module Name . 在应用目标building settings ,您可以找到您的Product Module Name

  2. In Objective-C file, #import "<ProductModuleName>-Swift.h" 在Objective-C文件中, #import "<ProductModuleName>-Swift.h"

Use Objective-C in Swift 在Swift中使用Objective-C

  1. Create bridge file, Choosing File > New > File > (iOS or OS X) > Source > Header File. 创建桥文件,选择文件>新建>文件>(iOS或OS X)>源>头文件。 The naming convention is <ProductModuleName>-Bridging-Header.h (Also, you could create a Swift in pure Objective-C project and XCode will prompt whether you need a bridge file) 命名约定是<ProductModuleName>-Bridging-Header.h (另外,您可以在纯Objective-C项目中创建Swift ,XCode将提示您是否需要桥文件)

  2. Import Objective-C file you want to use into the bridge file. 将要使用的Objective-C文件导入桥文件。

  3. Use it directly in Swift Code. 直接在Swift Code中使用它。

In appName-Bridging-Header.h you need to import required .h file and they will be available in swift code appName-Bridging-Header.h您需要导入所需的.h文件,它们将以swift代码提供

And in Objective-C file you need to import the generated header module_name-Swift.h which contains all the swift classes. 在Objective-C文件中,您需要导入生成的头文件module_name-Swift.h ,其中包含所有swift类。 This way swift classes will be available in objective c. 这样,快速课程将在目标c中提供。

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

相关问题 在ViewController.m中,如何通过字符串访问ViewController.h中声明的属性? - In ViewController.m, how can I access a property declared in ViewController.h by a string? Xcode 10.1 ViewController.h 与 ViewController.swift - Xcode 10.1 ViewController.h vs ViewController.swift 我在情节提要中的视图控制器将无法连接到viewcontroller.h文件或.m - My view controller in storyboard will not connect to the viewcontroller.h file or the .m 无法将控件从WebView拖动到ViewController.h吗? - Can not control-drag from WebView to ViewController.h? 如何切换回主视图,即ViewController.h“ MainStoryboard.storyboard的笔尖名称是什么? - How to switch back to the Main view i.e ViewController.h" what is the nib name for MainStoryboard.storyboard? 圆形Rect按钮连接到ViewController.h - Round Rect Button connecting to ViewController.h 从另一个UIViewController的.xib文件在ViewController.h中创建IBOutlets - creating IBOutlets in ViewController.h from another UIViewController's .xib file 无法从 ViewController Xcode 将“按钮”连接到 viewcontroller.h - Cannot connect "button" to viewcontroller.h from a ViewController Xcode 无法在UIImageView和ViewController.h之间创建连接 - Cannot create a connection between a UIImageView and the ViewController.h 如何在Pod项目ViewController中导入AppDelegate.h文件? - How to import AppDelegate.h file in Pod project ViewController?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM