First step: Create and configure Bridging-header.h
Swift and OC are mixed, first there must be a. h file, where you use Bridging-header.h and then set the project's build Settings--swift compiler--objective-c bridging The Header content is demoapp/bridging-header.h, which is related to the Bridging-header.h location, starting at the root of the project in Objective-c bridging The Header option writes the Bridging-header.h relative path.
Step Two: third-party project Dependencies
For third-party project dependencies, I initially planned to use CocoaPods, but the process was tortuous, and at last it was reported
ld:218 duplicate symbols for Architecture I386clang:error:linker command failed with exit code 1 (use-v to see Invocat Ion
I have no choice. To copy the third-party project source code into their own projects, you can also see my copy of the Afnetworking project, and then add the source to build phases--compile sources inside
Step Three: Modify bridging-header.h
write in Bridging-header.h #import "AFNetworking.h"
Fourth step: Call OC
We can invoke the functionality of a third-party project when the previous work is done.
//
// 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()
// 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)
let params: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
})
}
}
Fifth step: Run the interface
: Http://pan.baidu.com/s/1c0myIbA
Swift Basics-invoking third-party OC projects