文章轉(zhuǎn)自:http://www.itdecent.cn/p/9e13b40f4485
第一步:創(chuàng)建和配置Bridging-Header.h
Swift與OC進(jìn)行混編,首先要有一個(gè).h文件,這里使用Bridging-Header.h然后設(shè)置項(xiàng)目的Build Settings--Swift Compiler--Objective-C Bridging Header內(nèi)容為DemoApp/Bridging-Header.h,這個(gè)與Bridging-Header.h位置有關(guān),從項(xiàng)目的根目錄開(kāi)始在Objective-C Bridging Header選項(xiàng)里面寫(xiě)入Bridging-Header.h相對(duì)路徑。
image
第二步:第三方項(xiàng)目依賴(lài)
把第三方項(xiàng)目源碼拷貝到自己的項(xiàng)目里面,上圖也可以看到我拷貝的事AFNetworking項(xiàng)目,然后在把源碼加入到Build Phases--Compile Sources里面
image
第三步:修改Bridging-Header.h
在Bridging-Header.h中寫(xiě)入#import"AFNetworking.h"
第四步:調(diào)用OC
前面的工作做完后我們就可以調(diào)用第三方項(xiàng)目的功能了
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
})
}
}