[英]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接口。
In the app target building settings
, you could find your Product Module Name
. 在应用目标
building settings
,您可以找到您的Product Module Name
。
In Objective-C file, #import "<ProductModuleName>-Swift.h"
在Objective-C文件中,
#import "<ProductModuleName>-Swift.h"
Use Objective-C in Swift 在Swift中使用Objective-C
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将提示您是否需要桥文件)
Import Objective-C file you want to use into the bridge file. 将要使用的Objective-C文件导入桥文件。
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.