将0作为枚举选项传递给Swift中的Objective C函数 [英] Passing 0 as an enum option into Objective C functions in 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屋!