Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Passing 0 as an enum option into Objective C functions in Swift

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
like image 660
sudo Avatar asked Mar 15 '15 06:03

sudo


1 Answers

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
}
like image 199
rob mayoff Avatar answered Nov 11 '22 03:11

rob mayoff