Passing 0 as an enumeration parameter in an Objective-C function in Swift

There are Objective-C libraries with functions that take integer enumeration parameters as a parameter, but they expect you to pass 0 if you want to use the default parameters, as is usually the case. But in Swift, this is unacceptable because the library defines the type of enumeration. Is there a way around this by not adding the 0 enum option to the library and then generating the bridge code so that its ObjC hugs work in Swift?

Here is 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 indicate an error where it says options: 0 , because 'Int' is not convertible to SDWebImageOptions . I tried something like the following, but I get the same error:

 let emptyOptions:SDWebImageOptions = 0 
+5
source share
1 answer

With Swift 2, the syntax for option sets has been changed to use array literals. Therefore, if you want to pass any parameters, you pass an empty list:

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

Source: https://habr.com/ru/post/1215375/


All Articles