Swift calls Objective C's framework

Source: Internet
Author: User

Many GitHub libraries after many years of development, the source code are written by OC, so, using Swift to call OC Library is a problem inevitably encountered in the development, this article takes afnetworking as an example, explains how to call across languages.

The first step

Create an empty Project
Note that the language chooses Swift

Step Two, create the Bridging-header

way one  
Create an OC file and click OK &NBSP,
pop up if you want to create a bridging-header, click OK, this will generate three files, One of them is the swiftuseafnetworkingdemo-bridging-header.h  we want to use;

Span style= "FONT-SIZE:16PX;" >

mode two, create any header file and set it to Bridging-header in buildsetting
Create a header file named Bridge (any)

Set in Build setting

step three, install the library (to install the Cocoa Pod in advance)

Then, my habit is to use the command line to modify this file
To the catalog of the project
3.1 Initializing Pods

pod init
    • 1

3.2 Modifying Podfile

vim Podfile
    • 1

And then modify it to the 3.3 installation library. It's probably a long time waiting.


3.4 Reopen the project, note that the project to open the pod

Fourth step, import in header file

Fifth step, test

Full code

//Viewcontroller.swiftSwiftuseafnetworkingdemo//Created by Huangwenchen on 15/4/8.Copyright (c) 2015 Huangwenchen. All rights reserved.//Import Uikitclass Viewcontroller:uiviewcontroller {lazyvar Manager:afurlsessionmanager= {var configuration= Nsurlsessionconfiguration. Defaultsessionconfiguration ()var Deaultmanager= Afurlsessionmanager (sessionconfiguration:configuration);return deaultmanager; } () override Func Viewdidload () {Super. Viewdidload ()var URL= Nsurl (String"Http://jsonplaceholder.typicode.com/posts")var request= Nsurlrequest (Url:url!)var datatask= Manager. datataskwithrequest (Request, Completionhandler: {(response:nsurlresponse!, data:anyobject!, Error:nserror!) -> void in NSLog ( "%@", data.description)}) Datatask.resume ()} }

Test, the library was used successfully.

Swift calls Objective C's framework

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.