Swift和Objective-C混編注意事項

來源:互聯網
上載者:User

標籤:注意事項   normal   comm   over   uikit   ase   橋接   first   sub   

混編

混編也無非兩種情況,

在Objective - C工程或者檔案使用Swift的檔案;

在Swift工程或者檔案使用Objective - C檔案。

在混編的過程中最重要的兩個檔案:

1.橋接檔案

橋接檔案“ProjectName-Bridging-Header.h”,在首次建立其他檔案的時候,會自動產生。如果不小心刪除後,也可以手動添加,不過名字必須是“ProjectName-Bridging-Header.h”標頭檔(名稱組成:工程名-Bridging-Header.h),如果名字記不清也可以自己建立Header file後,在Targets→Build Settings→Swift Compiler - General→Objective-C Bridging Header設定檔路徑,這個檔案主要是Swift使用OC類時使用。

2.Objective-C Generated Interface Header Name檔案

這個檔案是混編時,系統產生的Swift檔案對應的Objective-C的標頭檔,具體可以在Targets→Build Settings→Swift Compiler - General→Objective-C Generated Interface Header Name進行配置,預設檔案名稱是工程名-Swift.h,一般不做改動。

在Objective - C工程或者檔案使用Swift的檔案

當在OC檔案中調用Swift檔案中的類的時候,首先在OC檔案中要加上 #import “

ProjectName-swift.h”(名字組成:工程名-swift)

這個檔案雖然在工程中看不到,但是她真實存在,編譯後,你可以按住Command+單擊該檔案名稱,就會看到具體產生的程式碼。

引入後,具體類的使用,直接按照OC的方式使用即可。

在Swift工程或者檔案使用Objective - C檔案

當在Swift中使用OC檔案的時候,只需在橋接檔案即projectName-Bridging-Header.h檔案中引入需要的標頭檔。

具體使用,按照對應的Swift文法結構來即可。

混編注意事項

對於需要混編的Swift類添加@objc聲明或繼承NSObject或NSObject的子類

 class TestClass {// 屬性// 實現}

如果要在Objective-C類中使用TestClass類,應當使用@objc加以聲明,或者將TestClass繼承自NSObject或NSObject的子類,否則,引入ProductName-Swift.h之後,程式找不到對應類。

使用第三方Framework

設定: target-->build setting -->Packaging -->Defines Module為 “Yes”;

然後,設定檔Target -> Build Phases -> Link Binary,添加要匯入的Framework;

最後,還是要配置橋接檔案,比如要使用 abc-lib.framework庫中的 abc.h 就要這樣配置:#import"abc-lib/abc.h";

Subclass子類問題

對於自訂的類而言,Objective-C的類,不能繼承自Swift的類,即要混編的OC類不能是Swift類的子類。反過來,需要混編的Swift類可以繼承自OC的類。 註解

OC宏檔案

如Swift檔案要使用OC中定義的宏,只能使用常量簡單宏檔案。

Swift專屬特性

Swift中有許多OC沒有的特性,比如,Swift有元組、為一等公民的函數、還有特有的枚舉類型。所以,要使用的混編檔案要注意Swift專屬屬性問題。

案例之Swift中使用OC的block

Swift中使用Closure不能使用Block作為屬性進行傳值,必須是初始化方法或函數。
Objective-C檔案中:

#import <UIKit/UIKit.h>typedef void (^Myblock)(NSString *arg); @interface FirViewController : UIViewController //@property (copy, nonatomic) Myblock myBlock; //這種作為公用參數的形式,如果在Swift類中去回調的話,是有問題的。提示沒有初始化方法,所以使用下面的以Block為參數的方法 - (void)transValue:(Myblock) block;@end

下面是.m檔案

#import "FirViewController.h" @implementation FirViewController - (void)viewDidLoad {     [super viewDidLoad];     self.view.backgroundColor = [UIColor whiteColor]; } - (void)transValue:(Myblock)block{     if (block)     {         block(@"firBack");     } } @end

在Swift檔案回調:

在Swift使用OC的類時,首先在橋接檔案中聲明oc的標頭檔
工程名-Bridging-Header.h這是建立Swift工程的情況下

import UIKit class ViewController: UIViewController {    override func viewDidLoad()     {         super.viewDidLoad()         self.view.backgroundColor = UIColor.whiteColor()     }     @IBOutlet weak var goFirst: UIButton!     @IBAction func goFirstAction(sender: AnyObject)     {         let firVC:FirViewController = FirViewController()         firVC. transValue { ( arg:String !) -> Void in             self.aBtn?.setTitle(arg, forState: UIControlState.Normal)        }         self.navigationController?.pushViewController(firVC, animated: true) }

Swift和Objective-C混編注意事項

相關文章

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.