我的应用程序中有一个代码,希望在iOS 13或更高版本上执行。因此,我正在使用标准的可用性检查:
if #available(iOS 13.0, *) {
return Color.systemGray6.resolvedColor(with: trait!)
} else {
return Color(red: 0.082, green: 0.118, blue: 0.161, alpha: 1.0)
}
Color
is a typealias which casts to UIColor
on iOS and NSColor
on macOS. I am kind of trying to create macOS version of my target with as little if..else
as possible.
The code above should work as NSColor
has many of the same init methods as UIColor
. The problem is that when I build my macOS target it complains about systemGray6
. So, for a reason unknown to me, macOS target passes #available(iOS 13.0, *)
check!
为什么会发生,如何预防呢?
When you use
if #available(iOS 13.0, *)
you're basically saying: do this on iOS 13.0 and later, and on all other operating systems - that's what the*
means.In your specific case, you'll need to exclude macOS, since NSColor does not have the
systemGrayX
getters: