简体   繁体   中英

Unable to call specific Objective-C class method in Swift

I am using AFNetworking 2.3.1 :

let request = NSURLRequest(URL: NSURL(string: "http://xxx.xxx.xxx/xxx")!)
var requestOperation = AFHTTPRequestOperation(request: request)
requestOperation.responseSerializer = AFImageResponseSerializer()

I have an error at third line using Swift 1.1 (Xcode 6.1 beta 2 build 6A1030):

'init()' is unavailable: superseded by import of -[NSObject init]

This line should looks like this on Objective-C:

requestOperation.responseSerializer = [AFImageResponseSerializer serializer];

I think this problem is related to Swift auto-bridging from Objective-C. Any ideas to solve this?

在此输入图像描述

UPDATE:

This way doesn't works:

AFImageResponseSerializer.serializer()

And error description is very nice:

'serializer()' is unavailable: use object construction 'AFHTTPResponseSerializer()'

UPDATE 2:

Right now I found a temporarily solution. I added this code to bridging header:

@interface AFImageResponseSerializer (CustomInit)
+ (instancetype)sharedSerializer;
@end

and code added to "bridging-header" implementation file:

@implementation AFImageResponseSerializer (CustomInit)
+ (instancetype)sharedSerializer {
    return [AFImageResponseSerializer serializer];
}
@end

And used it like this:

AFImageResponseSerializer.sharedSerializer()

It was caused by becoming stricter of conversion to the base class in XCode 6.1. So, you need upcasting, like below.

requestOperation.responseSerializer = AFImageResponseSerializer() as AFHTTPResponseSerializer

如果它是一个类方法,你可能想要AFImageResponseSerializer.serializer()

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM