Swift实战(2)--在工程中添加object-C的类或者第三方框架(附翻译)
2014-06-20 14:23
246 查看
原文地址: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.hto an older ObjC File) **
Step 1: Add Objective-C Implementation -- .m(添加.m文件到工程)
Add a.mfile to your class, and name it
CustomObject.m
Step 2: Add Bridging Header(建立桥接文件)
When adding your.mfile, you'll likely be hit with a prompt that looks like this:(自动弹出桥接文件选项)
![](http://i.stack.imgur.com/nakLZ.png)
Click YES !
If you did not see the prompt, or accidentally deleted your bridging header, add a new
.hfile to your project and name it
<#YourProjectName#>-Bridging-Header.h and reference it from your build settings(如果没有自动弹出,则手动添加桥接文件,命名规则<#YourProjectName#>-Bridging-Header.h,然后在BuildSettings中添加如下配置)
![](https://d262ilb51hltx0.cloudfront.net/max/939/1*ffqYzEoCLR52aWy4iefrCA.png)
Step 3: Add Objective-C Header -- .h(添加对应.h文件)
Add another.hfile and name it
CustomObject.h
Step 4: Build your Objective-C Class
InCustomObject.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到桥接文件)
InYourProject-Bridging-Header.h:
#import "CustomObject.h"
Step 6: Use your Object(在swift工程中就可以直接调用了)
InSomeSwiftFile.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.swiftfile 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
InSomeRandomClass.m:
#import "<#YourProjectName#>-Swift.h"
The file:
<#YourProjectName#>-Swift.hshould 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.swiftfile 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
InSomeRandomClass.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
.swiftfile to an older project and get error:
dyld: Library not loaded: @rpath/libswift_stdlib_core.dylib, try completely
相关文章推荐
- Swift实战(2)--在工程中添加object-C的类或者第三方框架
- 如何为工程添加一个Installshield不支持的语言--艾泽拉斯之海洋女神翻译
- BUG:InstallScript工程中添加Object报错
- OpenStack Object Storage Developer Guide/Swift官方API文档 -- 翻译 (三)
- IOS开发之添加第三方lib或从其他工程引入lib时的链接错误:ld: symbol(s) not found for architecture i386
- 编译Android时,添加或者删除system.img中第三方apk、更改Android系统默认语言 .
- 深以为然-为什么一些JAVA EE / J2EE 工程是效率低下或者至少是效率欠佳的(翻译)
- 编译Android时,添加或者删除system.img中第三方apk、更改Android系统默认语言 .
- OpenStack Object Storage Developer Guide/Swift官方API文档 -- 翻译 (一)
- Xcode工程添加第三方文件的详细分析 Create folder references for any added folders
- Xcode工程添加第三方文件的详细分析 Create folder references for any added folders(转)
- 编写苹果游戏中心应用程序(翻译 1.4 添加游戏工具包框架)
- 【转】如何将TapkuLibrary框架添加到Xcode4工程项目中
- 添加asihttprequest框架到工程中使用
- OpenStack Object Storage Developer Guide/Swift官方API文档 -- 翻译 (二)
- vc6.0无法使用“添加到工程”或者"打开文件"解决方法
- 在vs2010 添加Gtest 测试框架X86 到x64 工程建立: 步骤 1. 先编译Gtest源码,生成DEBUG 版本"gtestd.lib"与RELEASE版本"gtest.lib"。此处不明
- 编译Android时,添加或者删除system.img中第三方apk、更改Android系统默认语言
- OpenStack Object Storage Developer Guide/Swift官方API文档 -- 翻译 (五)
- Xcode工程添加第三方文件的详细分析 Create folder references for any added folders