160129关于xcode 7中object c与swift相互引用的问题。
2016-01-29 17:16
417 查看
一,在swift文件 中引用object c文件。
在swift文件中创建一个object c类TestSwift2OC,在弹出对话框(Would
you like to configure an Objective an Objective-C bridging header?)
选择Yes 这时会自动生成一个 项目名-Bridging-Header.h文件
在Bridging-Header.h文件中把TestSwift2OC.h文件加进来
#import "TestSwift2OC.h"
好了
,我们在swift中可以调用了,试一下
一,在object c文件 中引用swift文件。
类似的在object c 项目中引用swift 文件 ,生成一个 继承NSObject swift类。
但在Xcode 7.1, 7.2中必须要选中Yes 才能在"TestSwiftOC-Swift.h"加入上面的代码,不然只生成这个文件 没有把这段代码加进来
然后在object 就可以像普通的object c类一样引用swift类
在object c中引用Charts时一直没发找到用swift实现的类,最后找到官方文件,原来在同一个工程中引用swift和从另一个工程引用不一样需要
@import 另一个工程的frame
You can import external frameworks that have a pure Objective-C codebase, a pure Swift codebase, or a mixed-language codebase. The process for importing an external framework is the same whether the framework is written in a single language or contains files
from both languages. When you import an external framework, make sure the Defines Module build setting for the framework you’re importing is set to “Yes".
You can import a framework into any Swift file within a different target using the following syntax:
You can import a framework into any Objective-C
within a different target using the following syntax:
https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html#//apple_ref/doc/uid/TP40014216-CH10-XID_75
在swift文件中创建一个object c类TestSwift2OC,在弹出对话框(Would
you like to configure an Objective an Objective-C bridging header?)
选择Yes 这时会自动生成一个 项目名-Bridging-Header.h文件
#import <Foundation/Foundation.h> @interface TestSwift2OC : NSObject -(void)TestOCStr; @end
#import "TestSwift2OC.h" @implementation TestSwift2OC -(void)TestOCStr { NSLog(@"这个是OC方法"); } @end
在Bridging-Header.h文件中把TestSwift2OC.h文件加进来
#import "TestSwift2OC.h"
好了
,我们在swift中可以调用了,试一下
var testSwift2oc=TestSwift2OC() testSwift2oc.TestOCStr()
一,在object c文件 中引用swift文件。
类似的在object c 项目中引用swift 文件 ,生成一个 继承NSObject swift类。
import UIKit class TestOC2Swft: NSObject { func testSwftStr(){ println("这个是Swift方法") } }生成文件时同样会弹出在弹出对话框(Would you like to configure an Objective an Objective-C bridging header?),在xcode6中不需要选择yes.也会把这个swift类加进 系统自动生成的文件 。#import "TestSwiftOC-Swift.h"
SWIFT_CLASS("_TtC22TestObjectCImportSwift9FirtSwift") @interface TestOC2Swft : NSObject - (void)testSwftStr; - (nonnull instancetype)init OBJC_DESIGNATED_INITIALIZER; @end
但在Xcode 7.1, 7.2中必须要选中Yes 才能在"TestSwiftOC-Swift.h"加入上面的代码,不然只生成这个文件 没有把这段代码加进来
然后在object 就可以像普通的object c类一样引用swift类
TestOC2Swft *testOC2Swift=[[TestOC2Swft alloc]init]; [testOC2Swift testSwftStr];
在object c中引用Charts时一直没发找到用swift实现的类,最后找到官方文件,原来在同一个工程中引用swift和从另一个工程引用不一样需要
@import 另一个工程的frame
You can import external frameworks that have a pure Objective-C codebase, a pure Swift codebase, or a mixed-language codebase. The process for importing an external framework is the same whether the framework is written in a single language or contains files
from both languages. When you import an external framework, make sure the Defines Module build setting for the framework you’re importing is set to “Yes".
You can import a framework into any Swift file within a different target using the following syntax:
import FrameworkName
You can import a framework into any Objective-C
.mfile
within a different target using the following syntax:
@import FrameworkName;
https://developer.apple.com/library/prerelease/ios/documentation/Swift/Conceptual/BuildingCocoaApps/MixandMatch.html#//apple_ref/doc/uid/TP40014216-CH10-XID_75
相关文章推荐
- UiObject2 API的简单译文
- json_decode() expects parameter 1 to be string, object given
- 查询表达式和LINQ to Objects
- Animation动画详解(七)——ObjectAnimator基本使用
- 15.Object Manager
- Objective-C语法之基本数据类型
- a different object with the same identifier value was already associated with th
- 转:hibernate删除时的异常 deleted object would be re-saved by cascade (remove deleted ob
- 论文笔记:Faster R-CNN:Towards Real-Time Object Detection with Region Proposal Networks
- Objective-C MD5 And SHA And HMAC-SHA
- Object-C--->Swift之(九)延迟存储属性
- JSONObject.toBean(JSONObject jsonObject, Class beanClass)时,防止时间类型为系统默认时间
- Objective-C中的Block
- tableView视差滚动
- Swift项目兼容Objective-C问题汇总
- Objective-C语言编写(OC)打僵尸小游戏源码
- 在主线程中慎用WaitForSingleObject (WaitForMultipleObjects)
- Object-C--->Swift之(八)类和结构体
- 浅析Objective-C的copy
- Objective-C Runtime学习笔记