iOS開發——實用篇&Swift與Object-C混編之架構

來源:互聯網
上載者:User

標籤:

swift與OC混編之架構的建立和調用首先建立一個project取個名字叫“MyMixed”,選擇iOS-framework&library-cocoa touch framework然後在裡面建立一個SwiftView.swift檔案,一個objc的OCView檔案和MyOCView檔案三個檔案都繼承UIView首先在SwiftView裡調用OCView
 1 import UIKit 2  3   4  5 class SwiftView: UIView { 6  7   8  9     init(frame: CGRect) {10 11         super.init(frame: frame)12 13         self.backgroundColor = UIColor.redColor()14 15         16 17         var ocView = OCView(frame:CGRectMake(0,0,50,50))18 19         self.addSubview(ocView)20 21     }22 23  24 25 }26 27  

 

然後在MyOCView裡調用SwiftView

 1 @implementation MyOCView 2  3 - (instancetype)initWithFrame:(CGRect)frame 4  5 { 6  7     self = [super initWithFrame:frame]; 8  9     if (self) {10 11 self.backgroundColor = [UIColor grayColor];12 13         SwiftView *sView = [[SwiftView alloc] initWithFrame:CGRectMake(0, 0, 100, 100)];14 15         [self addSubview:sView];16 17     }18 19     return self;20 21  22 23 }

 

 

這時候編譯是會出錯的,因為不能互相找到路徑,這個時候要在工程MyMixed的build settings-swift compiler-objective-才 bridging header裡加入標頭檔路徑MyMixed/OCView.h讓SwiftView找到OCView.h

然後在OCView.m裡加入

//工程名加上編譯時間會產生的一個名子,這樣就可以使用swift檔案

#import "MyMixed/MyMixed-Swift.h"

就能通過編譯,這個時候混合架構就製作完成了

 

再重建立一個工程叫MixPro這裡我們使用swift語言

然後add files to“。。。”匯入架構的工程檔案(當然也可以直接匯入編譯好的framework),然後編譯一下架構工程,看是否能編譯通過,然後在MixPro工程的build phases裡點擊link binary with libraries 添加MyMixed.framework,這個時候架構添加完成,編譯看是否通過

然後在viewcontroller裡添加代碼

 1 import UIKit 2  3 import MyMixed 4  5   6  7 class ViewController: UIViewController { 8  9                             10 11     override func viewDidLoad() {12 13         super.viewDidLoad()14 15         // Do any additional setup after loading the view, typically from a nib.16 17         18 19         var swiftView = SwiftView(frame:CGRect(x:20, y:20, width:100, height:100))20 21         self.view.addSubview(swiftView)22 23         24 25         var myocView = MyOCView(frame:CGRectMake(50,140,200,200))26 27         self.view.addSubview(myocView)28 29     }30 31  32 33     override func didReceiveMemoryWarning() {34 35         super.didReceiveMemoryWarning()36 37         // Dispose of any resources that can be recreated.38 39     }40 41  42 43 }

 

這個時候會出錯,原因是找不到MyOCView,因為我們在建立MyMixed工程的時候選擇的swift,所以只有.swift檔案才能在外部被看見我們點擊MyMixed工程的MyMixed.h檔案可以看見

// In this header, you should import all the public headers of your framework using statements like #import

不難明白如果要這個framework的其他檔案被外部工程看見,我們需要#import 這個標頭檔,但是這個一定是PublicHeader.h所以我們點擊MyMixed工程的build phases裡的Headers,可以看見public裡只有MyMixed.h,我們要想在外部使用MyOCView.h就要把它從project裡拖到public裡,然後重新編譯,錯誤消除編譯通過

附上一張



 

 

 

 

 

 

iOS開發——實用篇&Swift與Object-C混編之架構

聯繫我們

該頁面正文內容均來源於網絡整理,並不代表阿里雲官方的觀點,該頁面所提到的產品和服務也與阿里云無關,如果該頁面內容對您造成了困擾,歡迎寫郵件給我們,收到郵件我們將在5個工作日內處理。

如果您發現本社區中有涉嫌抄襲的內容,歡迎發送郵件至: info-contact@alibabacloud.com 進行舉報並提供相關證據,工作人員會在 5 個工作天內聯絡您,一經查實,本站將立刻刪除涉嫌侵權內容。

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.