在Swift中实现Objective-C协议

7

我有一个Objective-C类中的协议:

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

还需一个与之连接的快速类:

class YTShare: UIViewController, YTManagerDelegate{

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

我收到了错误信息:type YTShare does not conform to protocol YTShareDelegate,可能是因为我错误地编写了 Swift 函数,所以 obj 类找不到它。怎样才能正确地编写它呢?

1个回答

6

委托方法中有两个错误

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

参数必须是非可选项,且 C 类型 int 映射到 Swift 中的 CIntInt32 的别名):

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

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


网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接