Swift调用Objective C的FrameWork
2015-11-15 17:11
585 查看
很多Github的库经过很多年的发展,源码都是OC写的,,所以,用Swift调用OC的库就是开发中难免遇到的的一个问题,本文以AFNetworking为例,讲解如何跨语言调用。
注意,语言选择Swift
![](http://www.helloswift.com.cn/uploads/allimg/150409/1-1504091F4124P.png)
这个文件的作用,就是把OC的接口暴露给Swift。有两种方式
方式一
创建一个OC文件,然后点击确定
![](http://www.helloswift.com.cn/uploads/allimg/150409/1-1504091F421596.png)
弹出是否要创建Bridging-Header,点击确定,这样会生成三个文件,其中的一个就是我们要用的SwiftUseAFNetworkingDemo-Bridging-Header.h
![](http://www.helloswift.com.cn/uploads/allimg/150409/1-1504091F430I6.png)
方式二,创建任意头文件,然后在buildSetting里设置为bridging-header
创建一个头文件,命名为bridge(任意)
![](http://www.helloswift.com.cn/uploads/allimg/150409/1-1504091F439618.png)
在build setting里设置
![](http://www.helloswift.com.cn/uploads/allimg/150409/1-1504091F44b60.png)
然后,我的习惯是用命令行来修改这个文件
到工程的目录
3.1初始化pod
1
3.2 修改PodFile
1
然后修改成如图
![](http://www.helloswift.com.cn/uploads/allimg/150409/1-1504091F500648.png)
3.3安装库 这里可能等的时间有点长,等着就是了
3.4 重新打开工程,注意,要打开Pod的工程
![](http://www.helloswift.com.cn/uploads/allimg/150409/1-1504091F511514.png)
![](http://www.helloswift.com.cn/uploads/allimg/150409/1-1504091F523441.png)
完整代码
测试,库使用成功。
第一步
创建一个空的工程注意,语言选择Swift
![](http://www.helloswift.com.cn/uploads/allimg/150409/1-1504091F4124P.png)
第二步,创建bridging-header
这个文件的作用,就是把OC的接口暴露给Swift。有两种方式方式一
创建一个OC文件,然后点击确定
![](http://www.helloswift.com.cn/uploads/allimg/150409/1-1504091F421596.png)
弹出是否要创建Bridging-Header,点击确定,这样会生成三个文件,其中的一个就是我们要用的SwiftUseAFNetworkingDemo-Bridging-Header.h
![](http://www.helloswift.com.cn/uploads/allimg/150409/1-1504091F430I6.png)
方式二,创建任意头文件,然后在buildSetting里设置为bridging-header
创建一个头文件,命名为bridge(任意)
![](http://www.helloswift.com.cn/uploads/allimg/150409/1-1504091F439618.png)
在build setting里设置
![](http://www.helloswift.com.cn/uploads/allimg/150409/1-1504091F44b60.png)
第三步,安装库(要提前安装Cocoa Pod)
然后,我的习惯是用命令行来修改这个文件到工程的目录
3.1初始化pod
pod init
1
3.2 修改PodFile
vim Podfile
1
然后修改成如图
![](http://www.helloswift.com.cn/uploads/allimg/150409/1-1504091F500648.png)
3.3安装库 这里可能等的时间有点长,等着就是了
pod install
3.4 重新打开工程,注意,要打开Pod的工程
![](http://www.helloswift.com.cn/uploads/allimg/150409/1-1504091F511514.png)
第四步,在头文件中import
![](http://www.helloswift.com.cn/uploads/allimg/150409/1-1504091F523441.png)
第五步,测试
完整代码// // ViewController.swift // SwiftUseAFNetworkingDemo // // Created by huangwenchen on 15/4/8. // Copyright (c) 2015年 huangwenchen. All rights reserved. // import UIKit class ViewController: UIViewController { lazy var 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() } }
测试,库使用成功。
相关文章推荐
- Object-C初体验
- Object-C初体验
- Object-C初体验
- NSObject 内省方法
- 初探swift语言的学习—Object-C与Swift混编
- Objective-C之集合对象的内存管理
- Objective-C:Foundation
- Objective-C:类别&代理
- Objective-C中的Block
- Objective-C:内存管理
- 关于oc运行时 isa指针详解
- Objective-C中的登录界面判断输入的内容
- 《Django学习》遇到的问题:“TypeError:'str' object is not callable"
- 基于Objective-C的iOS霓虹灯程序
- ios面向切面AOP(上) Objective-c版本
- 【Java相关】Java的Object类
- Objective-C多态,动态类型识别,动态绑定
- Objective-C 实例方法可见度,方法
- Objective-C中类属性的 copy, retain, assign , readonly , readwrite, nonatomic区别
- 在项目中同时使用Objective-C和Swift