在Swift中实现Objective c协议

Mar*_*rco 6 protocols objective-c swift

我在一个客观的c类中有这个协议:

@protocol YTManagerDelegate <NSObject>
@required
- (void)uploadProgressPercentage:(int)percentage;
@end
...
Run Code Online (Sandbox Code Playgroud)

和一个与之相关的快速课程:

class YTShare: UIViewController, YTManagerDelegate{

    func uploadProgressPercentage(percentage:Int?){
        println(percentage)
    }
    ...
Run Code Online (Sandbox Code Playgroud)

我收到错误:类型YTShare不符合协议YTShareDelegate,我可能错误地写了swift函数,所以obj类没有找到它.我怎么写得正确?

Mar*_*n R 5

委托方法中有两个错误

func uploadProgressPercentage(percentage:Int?){
    println(percentage)
}
Run Code Online (Sandbox Code Playgroud)

该参数不能是可选的,并且C类型int映射到Swift as CInt(别名Int32):

func uploadProgressPercentage(percentage:CInt){
    println(percentage)
}
Run Code Online (Sandbox Code Playgroud)

或者,您可以NSInteger在Objective-C协议中使用,该协议Int在Swift中映射.这将是32位或64位整数,具体取决于体系结构,而int/ CInt始终为32位.