文章轉自:http://www.reibang.com/p/9e13b40f4485
第一步:創(chuàng)建和配置Bridging-Header.h
Swift與OC進行混編唠梨,首先要有一個.h文件锦茁,這里使用Bridging-Header.h然后設置項目的Build Settings--Swift Compiler--Objective-C Bridging Header內容為DemoApp/Bridging-Header.h,這個與Bridging-Header.h位置有關胚吁,從項目的根目錄開始在Objective-C Bridging Header選項里面寫入Bridging-Header.h相對路徑撑毛。
image
第二步:第三方項目依賴
把第三方項目源碼拷貝到自己的項目里面书聚,上圖也可以看到我拷貝的事AFNetworking項目,然后在把源碼加入到Build Phases--Compile Sources里面
image
第三步:修改Bridging-Header.h
在Bridging-Header.h中寫入#import"AFNetworking.h"
第四步:調用OC
前面的工作做完后我們就可以調用第三方項目的功能了
import UIKit
classViewController: UIViewController {
@IBOutletweak varweatherInfo: UITextView!
override func viewDidLoad() {
super.viewDidLoad()
updateWeatherInfo()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
func updateWeatherInfo() {
let manager = AFHTTPRequestOperationManager()
let url ="http://api.openweathermap.org/data/2.5/weather"
println(url)
letparams:NSDictionary = ["lat":"37.785834","lon":"-122.406417","cnt":0]
println(params)
manager.GET(url,
parameters: params,
success: { (operation: AFHTTPRequestOperation!,
responseObject: AnyObject!) in
self.weatherInfo.text="JSON: "+ responseObject.description!
},
failure: { (operation: AFHTTPRequestOperation!,
error: NSError!) in
self.weatherInfo.text="Error: "+ error.localizedDescription
})
}
}