Swift与Objective-C交互
在同一個工程中是可以同時使用Swift和OC的,但不可以同時出現在同一個文件中。
OC調用Swift相關信息的方法
在***.m文件中導入工程名-Swift.h即可。
如工程名為ABC,則在需要使用Swift相關信息的.m文件中#import "ABC-Swift.h"即可。
Swift調用OC相關信息的方法
當在Swift/OC工程中創建OC/Swift文件時,會有提示,如下圖
點擊Yes,會創建一個工程名-Bridging-Header.h的文件,將Swift使用的相關OC的.h文件導入(#import "****.h")就可以在Swift中使用了。
參照:https://itunes.apple.com/us/book/using-swift-cocoa-objective/id888894773?mt=11
注意點
遇到問題
- 1、Swift繼承Objective-C并重載父類方法出現編譯異常
- 2、Swift中NSClassFromString方法對Swift的類無作用
- 3、Swift中以performSelector:開頭的方法、IMP、NSInvocation已經去掉了,用什么替代?
- 4、Swift中AnyObject如何轉換為閉包類型?
可能有未解決的問題,如果您有方法解決,請留言:) 無比感謝!
1、Swift繼承Objective-C并重載父類方法出現編譯異常
父類代碼:
@interface SuperClass : NSObject - (NSArray *) arrayWithString:(NSString *)string array:(NSArray *)array dictionary:(NSDictionary *)dictionary number:(NSNumber *)number; @end @implementation SuperClass - (NSArray *) arrayWithString:(NSString *)string array:(NSArray *)array dictionary:(NSDictionary *)dictionary number:(NSNumber *)number { return @[]; } @end在*-Bridging-Header.h文件中導入.h頭文件:#import "SuperClass.h"
子類代碼:
class SubClass: SuperClass { override func arrayWithString(string: NSString, array: NSArray, dictionary: NSDictionary, number: NSNumber) -> NSArray { return ["2"] } }有參數或者有返回的Objective-C方法被swift重載的時候就會有編譯異常:
Overriding method with selector '***' has incompatible type '****'解決方法:
如圖
可以在Quick Help中看到方法的參數和返回值的類型 上面子類代碼改為如下即可:
2、Swift中NSClassFromString方法對Swift的類無作用
代碼例子如上
在Swift類中使用
var superC: AnyClass! = NSClassFromString("SuperClass") println(NSStringFromClass(superC)) // 輸出SuperClass var subC: AnyClass! = NSClassFromString("SubClass") println(NSStringFromClass(subC)) // 輸出nil暫時解決辦法:
var subC: AnyClass! = SubClass.self println(NSStringFromClass(subC)) // 輸出SubClass解決方法:
在SubClass上增加@objc(SubClass),SubClass類變為:
3、Swift中以performSelector:開頭的方法、IMP、NSInvocation已經去掉了,用什么替代?
The performSelector: method and related selector-invoking methods are not imported in Swift because they are inherently unsafe. 摘自
Objective-C中的執行指定對象的指定方法:
- 使用以performSelector:開頭的方法;
- 使用IMP;
- 使用NSMethodSignature及NSInvocation,見Object-C中使用NSInvocation在Runtime時調用方法時傳入多個參數的方法
但Swift中以performSelector:開頭的方法、IMP、NSInvocation已經去掉了。。。
Swift的NSInvocationOperation類、NSObject的func forwardInvocation(anInvocation: NSInvocation!)都有對NSInvocation的引用,為毛點進去神馬都沒有呢。。。或許還是因為Xcode6 beta2還是beta版本吧。。。beta3也是如此。。。
那么用什么替代呢?
我目前還是使用Objective-C配合解決這個問題
Xcode6 beta3中的Swift加入了IMP,但和Objective-C中的IMP完全不一樣。還不知道怎么用。
4、Swift中AnyObject如何轉換為閉包類型?
Swift includes a protocol type named AnyObject that represents any kind of object, just as id does in Objective-C. The AnyObject protocol allows you to write type-safe Swift code while maintaining the flexibility of an untyped object. Because of the additional safety provided by the AnyObject protocol, Swift imports id as AnyObject.
Swift closures and Objective-C blocks are compatible, so you can pass Swift closures to Objective-C methods that expect blocks. Swift closures and functions have the same type, so you can even pass the name of a Swift function. 摘自
如上所述,Swift中的AnyObject等價于Objective-C中的id,Swift中的閉包等價于Objective-C中的Block。但在實際應用中卻有問題,比如Objective-C中id類型可以強制轉換為Block,如下:
但Swift中如果強制轉換就有error了,如下:
typealias BasicBlock = () -> Void func a() -> BasicBlock { func b(){ println("123213"); } return b; } var c:AnyObject = a(); // error遇到的問題是:Swift通過問題3返回了一個Block(閉包),但其返回出來的是id類型,在Swift中接收到的是AnyObject類型,需要轉換成指定的閉包類型,然后執行。
轉載于:https://www.cnblogs.com/Free-Thinker/p/4992693.html
總結
以上是生活随笔為你收集整理的Swift与Objective-C交互的全部內容,希望文章能夠幫你解決所遇到的問題。
- 上一篇: M2: XAML Controls(2)
- 下一篇: IOS中UITableViewCell的