Swift_Swift与objC之间的相互调用

假如你如今有一个 swift 类 LoginViewContrller.swift, 可是工程是objc的工程:AppDelegate.h/.mobjective-c

如今设置AppDelegate的rootviewcontroller为 LoginViewContrllerswift

1. 在target -> build setting -> Product Module Name , 能够写成你的项目名xcode


2. 在 AppDelegate.m 中添加
#import "xmppClient-Swift.h" ,其中 xmppClient 就是咱们刚才写的Product Module Name。 若是没有自动联想出来,能够直接手写上去, 我就是手写上去的,编译不会报错。ui

3. 接下来,你就能够在 didFinishLaunchingWithOptions 中使用你的 Swift 文件了code

这是个人代码:
get

self.window.rootViewController = [[UINavigationController alloc] initWithRootViewController:loginViewController];
it

4. 编译,运行。io


那若是我是Swift的工程, 要如何设置 rootviewcontroller 为 objc 文件呢?编译

我新建一个Swift工程 : LALALAclass

新建cocoa touch class -> language选objc

当最后create 的时候,xcode 会提示以下:

选 YES

发现工程里会另外多了一个 "productName"-Bridging-Header.h 的文件。 

只须要把你想在Swift中用的objc文件 import 进来。

例:

而后在 AppDelegate.Swift 中 写

        self.window!.rootViewController = RootViewController()

运行,能够了。


参考: http://stackoverflow.com/questions/24002369/how-to-call-objective-c-code-from-swift

相关文章
相关标签/搜索