繁体   English   中英

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

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

我试图创建一个文件.swift来改变导航栏中的颜色,但是当我试图在ViewController中导入File.swift时它会显示一个错误。

-------代码--------

//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.
    }
    */

错误说:预期; 在顶级声明之后

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

在Objective-C中使用Swift

您需要依赖Xcode生成的头文件将这些文件公开给Objective-C。 这个自动生成的文件是一个Objective-C头,它声明了目标中的Swift接口。

  1. 在应用目标building settings ,您可以找到您的Product Module Name

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

在Swift中使用Objective-C

  1. 创建桥文件,选择文件>新建>文件>(iOS或OS X)>源>头文件。 命名约定是<ProductModuleName>-Bridging-Header.h (另外,您可以在纯Objective-C项目中创建Swift ,XCode将提示您是否需要桥文件)

  2. 将要使用的Objective-C文件导入桥文件。

  3. 直接在Swift Code中使用它。

appName-Bridging-Header.h您需要导入所需的.h文件,它们将以swift代码提供

在Objective-C文件中,您需要导入生成的头文件module_name-Swift.h ,其中包含所有swift类。 这样,快速课程将在目标c中提供。

暂无
暂无

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

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