简体   繁体   中英

Swift multiple #available checks for bug in 2 versions of SDK

Apple had a bug in the SDK, which I needed to implement a workaround, they have since fixed that bug but not for two versions of the SDK. I would like the workaround to apply to just those two versions, but there does not seem to be a way of specifying that with #available . #available's intent is to check a version or later.

For example, there was a bug in 14.3 and 14.4, but that was fixed in 14.5, I would like the workaround to not be in effect after that. I came up with this, however, I am pretty sure that the second check is not going to work on 14.3. Is there a way to turn off the check after a version, or provide a range of versions? Also, you are not able to provide || or && the #available if or guard statements.

guard #available(iOS 14.3, *), #available(iOS 14.4, *) else { return }
// work around code. 

The best I can come up with is this, but that workaround will be there from 14.3 onward:

guard #available(iOS 14.3, *) else { return }
// work around code. 

The best answer I can think of is this:

if #available(iOS 14.5, *) {
} else if #available(iOS 14.3, *) {
    // workaround code
}

I know it's ugly, but it works. Unfortunately, Swift does not seem to provide a way to give a maximum value for #available .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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