简体   繁体   English

无法在Swift中调用特定的Objective-C类方法

[英]Unable to call specific Objective-C class method in Swift

I am using AFNetworking 2.3.1 : 我正在使用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): 我使用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: 在Objective-C上,这一行应如下所示:

requestOperation.responseSerializer = [AFImageResponseSerializer serializer];

I think this problem is related to Swift auto-bridging from Objective-C. 我认为这个问题与Objective-C的Swift自动桥接有关。 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: 更新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: 并将代码添加到“bridging-header”实现文件中:

@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. 它是由于在XCode 6.1中转换为基类变得更加严格。 So, you need upcasting, like below. 所以,你需要upcasting,如下所示。

requestOperation.responseSerializer = AFImageResponseSerializer() as AFHTTPResponseSerializer

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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