'SessionDelegate'没有名为'xxx'的成员 [英] 'SessionDelegate' does not have a member named 'xxx'
问题描述
我之前曾询问过如何使用可选方法创建协议的问题。几乎使用Objective-c的反应。请参阅此处。
I previously asked a question on how to create a protocol with optional methods. The response was, pretty much, use Objective-c. See it here.
我做了,一切正常,因为我正在使用
I did, and everything worked fine, because I was using
var delegate: SessionDelegate?;
也就是说,delegate是一个可选的SessionDelegate。为了从该委托中调用方法,我可以简单地写:
That is, "delegate" is an optional SessionDelegate. In order to call methods from that delegate I could simply write:
self.delegate?.willOpenSession?(self);
哪个没问题。但是,然后我,为什么让代表可选?所以我改变了它:
Which was fine. But then I though, why make the delegate optional? So I changed it:
var delegate: SessionDelegate;
显然,电话也会改变:
self.delegate.willOpenSession?(self);
问题是,最后一行(以及任何其他类型)说:'SessionDelegate'确实如此没有名为'willOpenSession'的成员。为什么这不起作用?
The problem is, this last line (and any other of the kind) says: 'SessionDelegate' does not have a member named 'willOpenSession'. Why doesn't this work?
这是我的代码:
SessionDelegate.h
SessionDelegate.h
@class Session;
@protocol SessionDelegate <NSObject>
@optional
- (BOOL)willOpenSession:(Session*)session;
- (void)didOpenSession:(Session*)session;
- (void)didFailOpenningSession:(Session*)session withError:(NSError*)error;
- (BOOL)willCloseSession:(Session*)session destroyingToken:(BOOL)destroyingToken;
- (void)didCloseSession:(Session*)session destroyingToken:(BOOL)destroyingToken;
@end
xxx-Bridging-Header.h
xxx-Bridging-Header.h
#import <Foundation/Foundation.h>
#import <FacebookSDK/FacebookSDK.h>
#import "SessionDelegate.h"
Session.swift
Session.swift
import Foundation
protocol Session {
var delegate: SessionDelegate { get set };
var isOpen: Bool { get };
var isCached: Bool { get };
func open();
func close();
func destroy();
}
FacebookSession.swift
FacebookSession.swift
import Foundation
class FacebookSession : Session {
var delegate: SessionDelegate;
var isOpen: Bool {
get {
return FBSession.activeSession().isOpen;
}
}
// TODO
var isCached: Bool {
get {
return false;
}
}
init(delegate: SessionDelegate) {
self.delegate = delegate;
}
func open() {
if self.isOpen {
return;
}
// Trigger the "will" event
self.delegate.willOpenSession?(self);
// Handle session state changes
var completionHandler: FBSessionStateHandler = {
session, status, error in
if error {
// Login failed for some reason, so we notify the delegate.
// TODO we should turn this NSError message into something more generic (that is, not facebook specific)
self.delegate.didFailOpenningSession?(self, withError: error);
}
else if status.value == FBSessionStateClosedLoginFailed.value {
// Login failed with no error. I'm not sure this ever happens
self.delegate.didFailOpenningSession?(self, withError: error);
}
else if status.value == FBSessionStateOpen.value || status.value == FBSessionStateOpenTokenExtended.value {
// Session is now open, we should notify the delegate
self.delegate.didOpenSession?(self);
}
else {
// There's no error but the session didn't open either. I don't think this ever happens, but if it does, what should we do here?
abort();
}
};
// Open the session, prompting the user if necessary
if FBSession.openActiveSessionWithReadPermissions([], allowLoginUI: true, completionHandler: completionHandler) {
// If we end up here, the session token must exist, se we now have an open session
self.delegate.d didOpenSession?(self);
}
}
func close() {
}
func destroy() {
}
}
祝你好运。
编辑:我还尝试了以下
self.delegate.willOpenSession?(self as Session);
和
self.delegate.willOpenSession(self as Session);
两者均无效。
编辑:之后所有,如果我使用它不起作用
After all, it doesn't work if I use
var delegate: SessionDelegate?;
编辑:之前的更改也会更改错误消息。现在我有可选的SessionDelegate(正如我在上一个编辑中所说)和以下行
The previous change also changes the error message. Now I have optional SessionDelegate (as I said in the previous edit) and the following line
self.delegate?.willOpenSession?(self)
说:找不到会员'willOpenSession'
Says: "Could not find member 'willOpenSession'"
推荐答案
您收到此错误是因为您将 Session
定义为协议,但Objective-C方法 SessionDelegate
协议中的定义需要 Session
类或子类。在Swift中,您可以在方法定义中将协议名称与类名互换使用,但在Objective-C中存在差异:
You're getting this error because you define Session
as a protocol, but the Objective-C method definitions in the SessionDelegate
protocol are expecting a Session
class or subclass. In Swift you can use protocol names interchangeably with class names in method definitions, but in Objective-C there's a difference:
- (void)willOpenSession:(Session *)session; // expects instance of Session class or subclass
- (void)willOpenSession:(id<Session>)session; // expects object conforming to Session protocol
从您发布的代码中我无法告诉您为什么' ve声明 Session
作为协议 - 它是否为不同的类添加功能?它可能是一个超级类 FacebookSession
(可能是其他会话)只是继承自?将 Session
更改为类(以及存根方法)可以解决问题。否则,您需要更改 SessionDelegate
方法以期望 id< Session>
,但对我来说,打破了编译器。
From the code you posted I can't tell why you've declared Session
as a protocol -- does it add functionality to disparate classes? Could it instead be a superclass that FacebookSession
(and presumably other sessions) simply inherits from? Changing Session
to a class (and stubbing the methods) would solve the issue. Otherwise you'll need to change your SessionDelegate
methods to expect an id<Session>
, but for me that broke the compiler.
这篇关于'SessionDelegate'没有名为'xxx'的成员的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!