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

相關文章
相關標籤/搜索