无法识别的选择器发送到 FBSDKProfile 类别上的实例

Unrecognized selector sent to instance on FBSDKProfile category

我在 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:]: 无法识别的选择器发送到实例

这是为什么?

可以使用,

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

参考了解更多详情。

问题

您已将属性 readonly 添加到您的 属性 profileImage。 这就是它所说的:

你只能读取它,所以调用setter会抛出异常。

解决方案

不要将 readonly 属性分配给 profileImage

@property (nonatomic, strong) UIImage *profileImage;