How could I type check CGColor / CGPath?

拥有回忆 提交于 2019-12-24 00:53:09

问题


So what it appears there is a filed bug for swift relating to the CoreFoundation types. Based on the description it appears there is not type checking for CGPath and CGColor, below is a snippet from the bug that demonstrates the behavior.

func check(a: AnyObject) -> Bool {
    return a is CGColor
}

check("hey") --> true
check(1.0) --> true
check(UIColor.redColor()) --> true

And here is what I'm trying to do

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
    }
}

I've done the following, by type checking type the types I know work first, then tagging on the last statement for AnyObject, and checking the CFTypeID. This works for now, but Apple Documentation says that the the CFTypeID can change, and should not be relied upon.

    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))
         }
   }

Has anyone found a reliable workaround a for this issue? As I do not want to use this hack as a long term solution


回答1:


Apple writes:

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).

That means that you should obtain the type ID at runtime, in your case:

if CFGetTypeID(currentValue) == CGColorGetTypeID() { ... }


来源:https://stackoverflow.com/questions/38252330/how-could-i-type-check-cgcolor-cgpath

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!