gpt4 book ai didi

ios - 'SessionDelegate' 没有名为 'xxx' 的成员

转载 作者:行者123 更新时间:2023-11-29 02:50:33 25 4
gpt4 key购买 nike

我之前问过一个关于如何使用可选方法创建协议(protocol)的问题。答案几乎是使用 Objective-c。参见 here

我做到了,一切正常,因为我正在使用

var delegate: SessionDelegate?;

也就是说,“delegate”是一个可选的SessionDelegate。为了从该委托(delegate)调用方法,我可以简单地编写:

self.delegate?.willOpenSession?(self);

这很好。但后来我想,为什么让委托(delegate)成为可选的呢?所以我改变了它:

var delegate: SessionDelegate;

显然,调用也发生了变化:

self.delegate.willOpenSession?(self);

问题是,最后一行(以及任何其他类似行)表示:“SessionDelegate”没有名为“willOpenSession”的成员。为什么这不起作用?

这是我的代码:

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

#import <Foundation/Foundation.h>
#import <FacebookSDK/FacebookSDK.h>
#import "SessionDelegate.h"

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

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);

两者都不起作用。

编辑:毕竟我用的话不行

var delegate: SessionDelegate?;

编辑:之前的更改也更改了错误消息。现在我有可选的 SessionDelegate (正如我在之前的编辑中所说)和以下行

self.delegate?.willOpenSession?(self)

说:“找不到成员‘willOpenSession’”

最佳答案

您收到此错误是因为您定义了 Session作为协议(protocol),但 Objective-C 方法定义在 SessionDelegate协议(protocol)期待一个 Session类或子类。在 Swift 中,您可以在方法定义中将协议(protocol)名称与类名称互换使用,但在 Objective-C 中存在差异:

- (void)willOpenSession:(Session *)session;    // expects instance of Session class or subclass
- (void)willOpenSession:(id<Session>)session; // expects object conforming to Session protocol

根据您发布的代码,我无法判断您为什么声明 Session作为协议(protocol)——它是否为不同的类添加了功能?它可以代替 FacebookSession 的父类(super class)吗? (可能还有其他 session )只是继承自?改变 Session到一个类(并 stub 方法)将解决这个问题。否则你需要改变你的 SessionDelegate期待 id<Session> 的方法,但对我来说这破坏了编译器。

关于ios - 'SessionDelegate' 没有名为 'xxx' 的成员,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24591921/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com