【问题标题】:incompatible pointer types sending class to parameter of type id fbsessiondelegate不兼容的指针类型将类发送到 id 类型为 fbsessiondelegate 的参数
【发布时间】:2012-09-12 03:58:33
【问题描述】:

在我的项目中添加了 Sharekit 组,并收到了很多编译警告。

在此它是一个静态实例,在静态方法中传递 self 参数是不正确的,因为在静态方法中没有此对象的特定实例。我该如何解决这个问题。

 + (void)logout
{
FBSession *fbSession; 

if(!SHKFacebookUseSessionProxy){
    fbSession = [FBSession sessionForApplication:SHKFacebookKey
                                             secret:SHKFacebookSecret
                                           delegate:self];

}else {
    fbSession = [FBSession sessionForApplication:SHKFacebookKey
                                    getSessionProxy:SHKFacebookSessionProxyURL
                                          delegate:self];
                  //delegate:[self sharedSomeProtocolDelegate]];
}

[fbSession logout];
}

任何人都请。

谢谢

【问题讨论】:

  • 忽略警告。 (在这种情况下)
  • 你说你有一个静态实例 - 你的意思是有一个 static 变量有一个指向你的类实例的指针?如果是这样,你能传递那个变量而不是self吗?

标签: iphone ios facebook sharekit


【解决方案1】:

做一个符合 FBSessionDelegate 协议的 SocialManager 对象。实例化它并在代码中给出而不是 self 参数。例如:

SocialManager.h:

 @interface SocialManager : NSObject <FBSessionDelegate>
- (id) init;
/**
 * Called when the user successfully logged in.
 */
- (void)fbDidLogin;

/**
 * Called when the user dismissed the dialog without logging in.
 */
- (void)fbDidNotLogin:(BOOL)cancelled;

/**
 * Called when the user logged out.
 */
- (void)fbDidLogout;
@end

SocialManager.m:

-(id) init
{
  self = [super init];
  return self;
}
- (void)fbDidLogin;
{
}
- (void)fbDidNotLogin:(BOOL)cancelled;
{
}
- (void)fbDidLogout
{
}

在你的代码中:

 + (void)logout
{
FBSession *fbSession;
SocialManager* socialManager = [[SocialManager alloc] init];

if(!SHKFacebookUseSessionProxy){
    fbSession = [FBSession sessionForApplication:SHKFacebookKey
                                             secret:SHKFacebookSecret
                                           delegate:socialManager];

}else {
    fbSession = [FBSession sessionForApplication:SHKFacebookKey
                                    getSessionProxy:SHKFacebookSessionProxyURL
                                          delegate:socialManager];
                  //delegate:[self sharedSomeProtocolDelegate]];
}

[fbSession logout];
}

这是一个简单的示例,但您必须在代码中实现自己的委托。只需将协议中的对象(可能是 viewController)添加到您的对象中,并将该对象设置为 FBsession 的委托。

【讨论】:

  • 有没有比这个更简单的解决方案。
  • 如果我使用 nil 而不是 self.我使用它并解决了问题,但我不确定在这种情况下它是否可行。
  • 你可以试试 put nil。什么都不应该发生,因为协议中没有“必需”的方法
【解决方案2】:

当使用静态类时,您可以手动消除这种情况下的警告:

...
delegate:(id<FBSessionDelegate>)self];

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多