Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Implement Objective c protocol in Swift

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?

like image 492
Marco Avatar asked Nov 05 '14 09:11

Marco


1 Answers

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.

like image 82
Martin R Avatar answered Nov 20 '22 17:11

Martin R