简体   繁体   中英

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.

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

Use Swift in Objective-C

you need to rely on an Xcode-generated header file to expose those files to Objective-C. This automatically generated file is an Objective-C header that declares the Swift interfaces in your target.

  1. In the app target building settings , you could find your Product Module Name .

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

Use Objective-C in Swift

  1. Create bridge file, Choosing File > New > File > (iOS or OS X) > Source > Header File. 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)

  2. Import Objective-C file you want to use into the bridge file.

  3. Use it directly in Swift Code.

In appName-Bridging-Header.h you need to import required .h file and they will be available in swift code

And in Objective-C file you need to import the generated header module_name-Swift.h which contains all the swift classes. This way swift classes will be available in objective c.

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