Implement Objective c protocol in Swift

♀尐吖头ヾ 提交于 2019-12-22 05:13:59

问题


I have this protocol in a objective c class:

@protocol YTManagerDelegate <NSObject>
@required
- (void)uploadProgressPercentage:(int)percentage;
@end
...

and a swift class connected to it:

class YTShare: UIViewController, YTManagerDelegate{

    func uploadProgressPercentage(percentage:Int?){
        println(percentage)
    }
    ...

I receive the error: type YTShare does not conform to protocol YTShareDelegate, I have probably write incorrectly the swift function so the obj class don't find it. How I can write it correctly?


回答1:


There are two errors in the delegate method

func uploadProgressPercentage(percentage:Int?){
    println(percentage)
}

The parameter must not be an optional, and the C type int is mapped to Swift as CInt (an alias for Int32):

func uploadProgressPercentage(percentage:CInt){
    println(percentage)
}

Alternatively, you could use NSInteger in the Objective-C protocol, which is mapped to Int in Swift. This would be a 32-bit or 64-bit integer, depending on the architecture, whereas int/CInt is always 32-bit.



来源:https://stackoverflow.com/questions/26753069/implement-objective-c-protocol-in-swift

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!