簡體   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