Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

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?

enter image description here

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()
like image 359
k06a Avatar asked Sep 17 '14 19:09

k06a


1 Answers

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
like image 80
yossy Avatar answered Sep 19 '22 14:09

yossy