Swift Basics-invoking third-party OC projects

Source: Internet
Author: User


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


Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

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.