如何在obj-c协议的swift中实现这个方法?

时间:2015-09-02 23:12:42

标签: ios objective-c swift

我已尝试过很多方法在swift中使用这个协议实现,编译器拒绝它们全部!

Objective C协议:

@protocol QBRTCClientDelegate <NSObject>


/**
 *  Called when started new session with you
 *
 *  @param session  QBWebRTCSession instance
 *  @param userInfo The user information dictionary for the new session. May be nil.
 */
- (void)didReceiveNewSession:(QBRTCSession *)session userInfo:(NSDictionary *)userInfo;

@optional
/**
 * Called when you called to user, but user does not respond
 * use +[QBRTCConfig setAnswerTimeInterval:value] to set answer time interval
 * default value: 45 seconds
 * @param userID ID of opponent
 */
- (void)session:(QBRTCSession *)session userDoesNotRespond:(NSNumber *)userID;

...

快速实施:

class QuickbloxManager: NSObject, QBRTCClientDelegate {
    func didReceiveNewSession(session: QBRTCSession, userInfo: NSDictionary) -> Void {

    }
    ...

编译器告诉我实现类不符合协议所以我一定做错了!

1 个答案:

答案 0 :(得分:1)

感谢评论中的帮助,我发现问题与NSDictionary类型中的不匹配有关。根据编译器的详细信息,实际错误是:

  

协议需要函数'didReceiveNewSession(_:userInfo :)'   type'(QBRTCSession!,userInfo:[NSObject:AnyObject]!) - &gt;无效“

这意味着到NSDictionary的映射不正确,而必须是Dictionary<NSObject, AnyObject>!类型。因此,swift中的新函数声明是:

func didReceiveNewSession(session: QBRTCSession!, userInfo: Dictionary<NSObject, AnyObject>!) -> Void {

    }

哪个编译没有错误。