[英]How can I implement this method in swift from obj-c protocol?
我已經嘗試了多種方法來快速表述此協議的實現,但編譯器拒絕了所有這些方法!
目標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 {
}
...
編譯器告訴我實現類不符合協議,因此我必須做錯了!
感謝評論中的幫助,我發現該問題與NSDictionary類型的不匹配有關。 根據編譯器的詳細信息,實際錯誤為:
協議要求函數'didReceiveNewSession(_:userInfo :)'類型為((QBRTCSession !, userInfo:[NSObject:AnyObject]!)-> Void'
這意味着到NSDictionary的映射不正確,而必須是Dictionary<NSObject, AnyObject>!
。 因此,迅速的新函數聲明是:
func didReceiveNewSession(session: QBRTCSession!, userInfo: Dictionary<NSObject, AnyObject>!) -> Void {
}
哪個編譯沒有錯誤。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.