将0作为枚举选项传递给Swift中的Objective C函数 [英] Passing 0 as an enum option into Objective C functions in Swift

查看:99
本文介绍了将0作为枚举选项传递给Swift中的Objective C函数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有一些Objective-C库具有整数枚举选项作为参数的函数,但是如果您希望使用默认选项,那么它们希望通过0。但是在Swift中,这是不允许的,因为库指定枚举类型。有没有办法在这个简单的添加一个0枚举选项到库,然后使桥接代码使其ObjC枚举在Swift工作?



这里有一个例子,$在iPhone应用程序中的c $ c> SDWebImageManager

  SDWebImageManager.sharedManager()。downloadWithURL(url ,options:0,progress:nil){(image:UIImage !, error:NSError !, cacheType:SDImageCacheType,finished:Bool) - > 
//块中的代码
}

Xcode将指出选项:0 的错误,因为'Int'不能转换为SDWebImageOptions 。我已经尝试过类似以下内容,但是我收到相同的错误:

  let emptyOptions:SDWebImageOptions = 0 


解决方案

从Swift 2起,选项集的语法已更改为使用数组文字。所以如果你想传递任何选项,你传递一个空列表:

  SDWebImageManager.sharedManager()。downloadWithURL(url,options :[],progress:nil){(image:UIImage !, error:NSError !, cacheType:SDImageCacheType,finished:Bool) - >空虚
//这里的代码
}


There are Objective-C libraries with functions that take integer enum options as a parameter, but they expect you to pass 0 in if you want default options, as is typical. But in Swift, that is not allowed because the library specifies an enum type. Is there any way around this short of adding a 0 enum option to the libraries then making bridging code to make its ObjC enums work in Swift?

Here's an example with SDWebImageManager in an iPhone app:

SDWebImageManager.sharedManager().downloadWithURL(url, options: 0, progress: nil) { (image:UIImage!, error:NSError!, cacheType:SDImageCacheType, finished:Bool) -> Void in
            // block code here
        }

Xcode will point out an error where it says options: 0 because 'Int' is not convertible to SDWebImageOptions. I've tried something like the following, but I get the same error:

let emptyOptions:SDWebImageOptions = 0

解决方案

As of Swift 2, the syntax for option sets was changed to use array literals. So if you want to pass no options, you pass an empty list:

SDWebImageManager.sharedManager().downloadWithURL(url, options: [], progress: nil) { (image:UIImage!, error:NSError!, cacheType:SDImageCacheType, finished:Bool) -> Void in
    // code here
}

这篇关于将0作为枚举选项传递给Swift中的Objective C函数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆