【问题标题】:Unrecognized selector sent to instance on FBSDKProfile category无法识别的选择器发送到 FBSDKProfile 类别上的实例
【发布时间】:2016-04-16 19:47:41
【问题描述】:

我在我的 iOS 应用中使用 Facebook v4 SDK。为了获取相关信息,我经常使用[FBSDKProfile currentProfile] 单例。但是,我还需要易于访问的个人资料图片,因此写了一个类别来处理这个问题。

这是头文件:

#import <FBSDKCoreKit/FBSDKCoreKit.h>

@interface FBSDKProfile (ProfileImage)

+(void)fetchProfileImageWithBlock:(void (^)(BOOL succeeded))handler;

@property (nonatomic, strong, readonly) UIImage *profileImage;

@end

这是实现文件:

#import "FBSDKProfile+ProfileImage.h"

@interface FBSDKProfile()

@property (nonatomic, strong, readwrite) UIImage *profileImage;

@end

@implementation FBSDKProfile (ProfileImage)

+(void)fetchProfileImageWithBlock:(void (^)(BOOL succeeded))handler {
    FBSDKProfile *currentProfile = [FBSDKProfile currentProfile];
    NSString *userId = currentProfile.userID;
    if (![userId isEqualToString:@""] && userId != Nil)
    {
        [self downloadFacebookProfileImageWithId:userId completionBlock:^(BOOL succeeded, UIImage *profileImage) {
            currentProfile.profileImage = profileImage;
            if (handler) { handler(succeeded); }
        }];
    } else
    {
        /* no user id */
        if (handler) { handler(NO); }
    }
}

+(void)downloadFacebookProfileImageWithId:(NSString *)profileId completionBlock:(void (^)(BOOL succeeded, UIImage *profileImage))completionBlock
{
    NSURL *url = [NSURL URLWithString:[NSString stringWithFormat:@"https://graph.facebook.com/%@/picture?type=large", profileId]];
    NSMutableURLRequest *request = [NSMutableURLRequest requestWithURL:url];
    [NSURLConnection sendAsynchronousRequest:request
                                   queue:[NSOperationQueue mainQueue]
                       completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
                               if (!error)
                               {
                                   UIImage *image = [[UIImage alloc] initWithData:data];
                                   completionBlock(YES, image);
                               } else{
                                   completionBlock(NO, nil);
                               }
                           }];
}

@end

但是,我遇到了这个异常:

由于未捕获的异常 'NSInvalidArgumentException' 导致应用程序终止,原因:'-[FBSDKProfile setProfileImage:]: unrecognized selector sent to instance

这是为什么?

【问题讨论】:

  • 我想你是在下载完成之前设置图像。
  • 属性不会在类别中自动合成。您必须编写自己的 getter 和 setter,并为图像提供自己的存储空间。
  • @dan 你能详细说明一下吗?尝试合成它会显示此错误:@synthesize not allowed in a category's implementation

标签: ios objective-c facebook facebook-sdk-4.0 objective-c-category


【解决方案1】:

问题

您已将属性readonly 添加到您的属性profileImage。 它的意思是:

您只能读取它,因此调用 setter 会引发异常。

解决方案

不要将readonly 属性分配给profileImage

@property (nonatomic, strong) UIImage *profileImage;

【讨论】:

  • 哦,我明白了。我使用的是[FBSDKProfile currentProfile].profileImage = profileImage;,而不是self.profileImage = profileImage,如果它不是一个单例而是一个“普通”类实例,这是可能的吗?
  • 如果属性是readwrite,它会起作用,因为它是默认值,所以您实际上不需要明确指定
  • 现在删除了readonly 属性,但它仍然显示相同的异常。它发出如下警告:“属性 'profileImage' 需要定义方法 profileImage - 使用 @dynamic 或提供此类别中的方法实现”。我不能做@synthesize,因为它显示一个错误。那么如何创建 setter/getter 呢?
  • 您是否尝试按照建议添加@dynamic profileImage;
  • 我做到了,虽然抑制了警告,但结果相同
【解决方案2】:

你可以使用,

FBSDKProfilePictureView *profilePictureview = [[FBSDKProfilePictureView alloc]initWithFrame:_imageView.frame];
[profilePictureview setProfileID:result[@"id"]];
[self.view addSubview:profilePictureview];

更多详情请参考this link

【讨论】:

    猜你喜欢
    • 2019-08-28
    • 2012-07-24
    相关资源
    最近更新 更多