swift 調(diào)用OC第三方AFNetWorking

文章轉(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

})

}

}
?著作權(quán)歸作者所有,轉(zhuǎn)載或內(nèi)容合作請(qǐng)聯(lián)系作者
【社區(qū)內(nèi)容提示】社區(qū)部分內(nèi)容疑似由AI輔助生成,瀏覽時(shí)請(qǐng)結(jié)合常識(shí)與多方信息審慎甄別。
平臺(tái)聲明:文章內(nèi)容(如有圖片或視頻亦包括在內(nèi))由作者上傳并發(fā)布,文章內(nèi)容僅代表作者本人觀點(diǎn),簡(jiǎn)書(shū)系信息發(fā)布平臺(tái),僅提供信息存儲(chǔ)服務(wù)。

相關(guān)閱讀更多精彩內(nèi)容

友情鏈接更多精彩內(nèi)容