Swift实战(2)--在工程中添加object-C的类或者第三方框架(附翻译)
原文地址:http://stackoverflow.com/questions/24002369/how-to-call-objective-c-code-from-swift
Using Objective-C Classes in Swift(在swift中使用oc)
** If you have an existing class that you'd like to use, perform Step 2 and then skip to Step 5. (For some cases, I had to add an explicit #import <Foundation/Foundation.h
to an older ObjC File) **
Step 1: Add Objective-C Implementation -- .m(添加.m文件到工程)
Add a .m
file to your class, and name it CustomObject.m
Step 2: Add Bridging Header(建立桥接文件)
When adding your .m
file, you'll likely be hit with a prompt that looks like this:(自动弹出桥接文件选项)
Click YES !
If you did not see the prompt, or accidentally deleted your bridging header, add a new .h
file to your project and name it <#YourProjectName#>-Bridging-Header.h and reference it from your build settings(如果没有自动弹出,则手动添加桥接文件,命名规则<#YourProjectName#>-Bridging-Header.h,然后在BuildSettings中添加如下配置)
Step 3: Add Objective-C Header -- .h(添加对应.h文件)
Add another .h
file and name it CustomObject.h
Step 4: Build your Objective-C Class
In CustomObject.h
#import <Foundation/Foundation.h>
@interface CustomObject : NSObject
@property (strong, nonatomic) id someProperty;
- (void) someMethod;
@end
In CustomObject.m
#import "CustomObject.h"
@implementation CustomObject
- (void) someMethod {
NSLog(@"SomeMethod Ran");
}
@end
Step 5: Add Class to Bridging-Header(将.h文件import到桥接文件)
In YourProject-Bridging-Header.h
:
#import "CustomObject.h"
Step 6: Use your Object(在swift工程中就可以直接调用了)
In SomeSwiftFile.swift
:
var instanceOfCustomObject: CustomObject = CustomObject()
instanceOfCustomObject.someProperty = "Hello World"
println(instanceOfCustomObject.someProperty)
instanceOfCustomObject.someMethod()
No need to import explicitly, that's what the bridging header is for.
Using Swift Classes in Objective-C(在oc中使用swift)
Step 1: Create New Swift Class
Add a .swift
file to your project, and name it MySwiftObject.swift
In MySwiftObject.swift
:
import Foundation
class MySwiftObject : NSObject {
var someProperty: AnyObject = "Some Initializer Val"
init() {}
func someFunction(someArg:AnyObject) -> String {
var returnVal = "You sent me \(someArg)"
return returnVal
}
}
Step 2: Import Swift Files to ObjC Class
In SomeRandomClass.m
:
#import "<#YourProjectName#>-Swift.h"
The file:<#YourProjectName#>-Swift.h
should already be created automatically in your project, even if you can not see it.
Step 3: Use your class
MySwiftObject * myOb = [MySwiftObject new];
NSLog(@"MyOb.someProperty: %@", myOb.someProperty);
myOb.someProperty = @"Hello World";
NSLog(@"MyOb.someProperty: %@", myOb.someProperty);
NSString * retString = [myOb someFunction:@"Arg"];
NSLog(@"RetString: %@", retString);
Using PURE Swift Classes in Objective-C
As pointed out by @TomášLinhart in the comments, "To be accessible and usable in Objective-C, a Swift class must be a descendant of an Objective-C class or it must be marked @objc
." Because our first example is a descendant of NSObject
, the compiler does this automatically. Let's look at an example class that is not a descendant of an Objective-C Class.
Step 1: Create New Swift Class
Add a .swift
file to your project, and name it PureSwiftObject.swift
In PureSwiftObject.swift
:
import Foundation
// Note '@objc' prefix
@objc class PureSwiftObject {
var name: String
init(name: String) {
self.name = name
}
// Needed to add a class level initializer
class func newInstanceNamed(name: String) -> PureSwiftObject {
return PureSwiftObject(name: name)
}
// Just a method for demonstration
func someMethod() {
println("Some method ran in pure swift object")
}
}
For this, I create a class initializer called 'newInstanceNamed:'. Because this class is no longer a descendent of NSObject
, it no longer has access to 'alloc' or 'new'. Perhaps there is another workaround, but this is the only way that I have found. I didn't find any explicit mention of this in the docs. If you do, and it contradicts my approach, please tell me and I'll update the answer to conform to the suggested style.
Step 2: Import Swift Files to ObjC Class
In SomeRandomClass.m
:
#import "<#YourProjectName#>-Swift.h"
(if you haven't already done so)
Step 3: Use your pure swift class
PureSwiftObject * pureSwiftObject = [PureSwiftObject newInstanceNamed:@"Janet"];
NSLog(@"PureSwiftNamed: %@", pureSwiftObject.name);
[pureSwiftObject someMethod];
Note:
1. CodeCompletion wasn't behaving as accurately as I'd like it to. On my system, running a quick build w/ "cmd + r" seemed to help Swift find some of the Objc code and vice versa.
2. If you add .swift
file to an older project and get error: dyld: Library not loaded: @rpath/libswift_stdlib_core.dylib
, try completely
Swift实战(2)--在工程中添加object-C的类或者第三方框架(附翻译)的更多相关文章
- ARC工程中添加非ARC文件
转载自:http://blog.csdn.net/zhenweicao/article/details/16988543 分类: IOS2013-11-27 17:02 626人阅读 评论(0) 收藏 ...
- 工程中添加工程依赖 Xcode iOS
有时我们需要在一个主工程中添加其他的子工程,用来对子工程进行编写修改或者是利用子工程中的库文件等等操作,这时候我们需要用到工程的嵌套. 步骤:(看图说话) 1.新建主工程,名为TestTTTT ...
- IOS学习:在工程中添加百度地图SDK
1.将下载下来的sdk中的inc文件夹.mapapi.bundle.libbaidumapapi.a添加到工程中,其中libbaiduapi.a有两个,一个对应模拟器一个对应真机,导入方法如下: 第一 ...
- 关于在工程中添加新文件时的LNK2019错误的一个解决办法
我这几天一直在研究Qt的串口程序,在读懂了官方给出的实例程序后我决定把其多线程的串口监视程序加入到我自己的工程中,便直接把问价复制到自己的工程下面,在Qt中加入到自己的工程中,但是总是出现LNK201 ...
- step6----->往工程中添加spring boot项目------->修改pom.xml使得我的project是基于spring boot的,而非直接基于spring framework
文章内容概述: spring项目组其实有多个projects,如spring IO platform用于管理external dependencies的版本,通过定义BOM(bill of mater ...
- 在工程中添加pch文件
在Xcode6之前,新建一个工程的时候,系统会帮我们自动新建一个以工程名为名字的pch (precompile header)文件,在开发过程中,可以将那些整个工程都广泛使用的头文件包含在该文件下,编 ...
- WPF - 为什么不能往Library的工程中添加WPF window
项目中添加一个Library 工程,但是却无法加入WPF window, WPF customize control. 调查了一下,发现这一切都由于Library工程中没有:ProjectTypeGu ...
- VS工程中添加c/c++工程中外部头文件及库的基本步骤
转载自 在VS工程中,添加c/c++工程中外部头文件及库的基本步骤: 1.添加工程的头文件目录:工程---属性---配置属性---c/c++---常规---附加包含目录:加上头文件存放目录. 2.添加 ...
- Visual Studio 向工程中添加文件夹
将要添加的文件夹拷贝到工程的目标文件夹中. 打开工程,在Solution Explorer中选中“Show All Files”按钮. 然后VS会显示文件夹中包含,但是不在工程中的文件夹. 右键该文件 ...
随机推荐
- 数学之路-python计算实战(18)-机器视觉-滤波去噪(双边滤波与高斯滤波 )
高斯滤波就是对整幅图像进行加权平均的过程.每个像素点的值,都由其本身和邻域内的其它像素值经过加权平均后得到.高斯滤波的详细操作是:用一个模板(或称卷积.掩模)扫描图像中的每个像素.用模板确定的邻域内像 ...
- MapReduce----K-均值聚类算法
对于K-均值聚类算法MapReduce的过程理解例如以下: 如果有个Mapper,首先把数据集分为个子集,分布到个Mapper上,初始化..并同一时候广播到H个Mapper上. E步: 在第一台Map ...
- bzoj1831: [AHOI2008]逆序对(DP+双精bzoj1786)
1831: [AHOI2008]逆序对 Description 小可可和小卡卡想到Y岛上旅游,但是他们不知道Y岛有多远.好在,他们找到一本古老的书,上面是这样说的: 下面是N个正整数,每个都在1~K之 ...
- Nginx搭建图片服务器
Nginx搭建图片服务器 标签(空格分隔): linux,nginx Nginx常用命令 ./nginx 启动 ./nginx -s reload 重载配置文件 ./nginx -s stop|sta ...
- BZOJ 3931 Dijkstra+网络流
思路: (我能说按照题意模拟么) 用long long inf 要开大--. //By SiriusRen #include <queue> #include <cstdio> ...
- java9新特性-9-语法改进:try语句
1. 使用举例 在java8 之前,我们习惯于这样处理资源的关闭: java 8 中,可以实现资源的自动关闭,但是要求执行后必须关闭的所有资源必须在try子句中初始化,否则编译不通过.如下例所 ...
- [NOI2003]逃学的小孩(树的直径)
[NOI2003]逃学的小孩 题目描述 Chris家的电话铃响起了,里面传出了Chris的老师焦急的声音:"喂,是Chris的家长吗?你们的孩子又没来上课,不想参加考试了吗?"一听 ...
- Java基础学习总结(26)——JNDI入门简介
JNDI是 Java 命名与目录接口(Java Naming and Directory Interface),在J2EE规范中是重要的规范之一,不少专家认为,没有透彻理解JNDI的意义和作用,就没有 ...
- 洛谷 P1583 魔法照片
P1583 魔法照片 题目描述 一共有n(n≤20000)个人(以1--n编号)向佳佳要照片,而佳佳只能把照片给其中的k个人.佳佳按照与他们的关系好坏的程度给每个人赋予了一个初始权值W[i].然后将初 ...
- Android设计模式(十二)--抽象工厂模式
问题: 抽象工厂模式,是一个,狠恶心的模式,那么这个模式在Android有没实用到过呢? 1.定义: 抽象工厂模式:为创建一组相关或者是相互依赖的对象提供一个接口,而不须要指定他们的详细类. 2.使用 ...