因此,它所显示的是一个与为斯威夫特归档错误类型相关的CoreFoundation。根据描述,没有对CGPath和CGColor进行类型检查,下面是演示该行为的bug的片段。
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
}
}我已经完成了以下操作,通过类型检查,首先输入我所知道的类型,然后对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))
}
}有人为这个问题找到了可靠的解决办法吗?因为我不想用这个黑客作为长期的解决方案
发布于 2016-07-07 18:16:39
因为类型ID的值可以在不同版本之间更改,所以您的代码不应该依赖存储的或硬编码的类型ID,也不应该硬编码任何类型ID的可观察属性(例如,它是一个小整数)。
这意味着您应该在运行时获得类型ID,在您的情况下:
if CFGetTypeID(currentValue) == CGColorGetTypeID() { ... }https://stackoverflow.com/questions/38252330
复制相似问题