Swift基礎--調用第三方OC項目

來源:互聯網
上載者:User
第一步:建立和配置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相對路徑。


第二步:第三方項目依賴

對於第三方項目的依賴,一開始我打算用CocoaPods,但是過程曲折,最後一直報

[objc]  view plain  copy   <span style="font-size:14px;">ld: 218 duplicate symbols for architecture i386   clang: error: linker command failed with exit code 1 (use -v to see invocation)</span>   我也沒有辦法了就把第三方項目源碼拷貝到自己的項目裡面,上圖也可以看到我拷貝的事AFNetworking項目,然後在把源碼加入到Build Phases--Compile Sources裡面

第三步:修改Bridging-Header.h

在Bridging-Header.h中寫入#import "AFNetworking.h" 第四步:調用OC

前面的工作做完後我們就可以調用第三方項目的功能了

[objc]  view plain  copy   //   //  ViewController.swift   //  DemoApp   //   //  Created by jiezhang on 14/10/24.   //  Copyright (c) 2014年 jiezhang. All rights reserved.   //      import UIKit      class ViewController: UIViewController {          @IBOutlet weak var weatherInfo: UITextView!       override func viewDidLoad() {           super.viewDidLoad()           updateWeatherInfo()       }          override func didReceiveMemoryWarning() {           super.didReceiveMemoryWarning()          

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.