我怎么能打字检查CGColor / CGPath?
所以看起来有一个与CoreFoundationtypes有关的swift的提交错误 。 根据描述,看起来没有CGPath和CGColor的types检查,下面是从错误中演示的行为的片段。
func check(a: AnyObject) -> Bool { return a is CGColor } check("hey") --> true check(1.0) --> true check(UIColor.redColor()) --> true
这就是我想要做的
if let value = self.valueForKeyPath(keyPath) { if let currentValue = value as? CGColor { // Do Something with CGColor } else if let currentValue = value as? CGSize { // Do Something with CGSize } else if let currentValue = value as? CGPoint { // Do Something with CGPoint } }
我已经完成了以下工作,通过types检查types我知道工作的types,然后标记在AnyObject的最后一个语句,并检查CFTypeID。 这个工作现在,但苹果文件说,CFTypeID可以改变,不应该依靠。
if let currentValue = value as? CGPoint { // Do Something with CGPoint } else if let currentValue = value as? CGSize { // Do Something with CGSize } else if let currentValue = value as? AnyObject { if CFGetTypeID(currentValue) == 269 { // Cast and do Something with CGColor methodCall((currentValue as! CGColor)) } }
有没有人find一个可靠的解决方法一个这个问题? 因为我不想使用这个黑客作为一个长期的解决scheme
苹果写道:
由于typesID的值可能会随发行版本而变化,因此您的代码不应该依赖于存储的或硬编码的types标识符,也不应该对types标识符的任何观察属性进行硬编码(例如,它是小整数)。
这意味着你应该在运行时获得typesID,在你的情况下:
if CFGetTypeID(currentValue) == CGColorGetTypeID() { ... }