Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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

like image 583
Emmanuel Zoolander Avatar asked Sep 27 '22 19:09

Emmanuel Zoolander


1 Answers

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.

like image 111
Elliot Li Avatar answered Sep 30 '22 08:09

Elliot Li