我如何输入检查 CGColor / CGPath?
How could I type check CGColor / CGPath?
所以看起来有一个 filed bug for swift 与 CoreFoundation 类型有关。根据描述,似乎没有对 CGPath 和 CGColor 进行类型检查,下面是演示该行为的错误片段。
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。这目前有效,但 Apple Documentation 表示 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))
}
}
有没有人找到解决此问题的可靠解决方法?因为我不想将此 hack 用作长期解决方案
Because the value for a type ID can change from release to release, your code should not rely on stored or hard-coded type IDs nor should it hard-code any observed properties of a type ID (such as, for example, it being a small integer).
这意味着您应该在运行时获取类型 ID,在您的情况下:
if CFGetTypeID(currentValue) == CGColorGetTypeID() { ... }
所以看起来有一个 filed bug for swift 与 CoreFoundation 类型有关。根据描述,似乎没有对 CGPath 和 CGColor 进行类型检查,下面是演示该行为的错误片段。
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。这目前有效,但 Apple Documentation 表示 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))
}
}
有没有人找到解决此问题的可靠解决方法?因为我不想将此 hack 用作长期解决方案
Because the value for a type ID can change from release to release, your code should not rely on stored or hard-coded type IDs nor should it hard-code any observed properties of a type ID (such as, for example, it being a small integer).
这意味着您应该在运行时获取类型 ID,在您的情况下:
if CFGetTypeID(currentValue) == CGColorGetTypeID() { ... }