如何输入检查CGColor/CGPath?

问题描述:

所以看来,有一个与CoreFoundation类型有关的提起的Swift漏洞.根据描述,似乎没有针对CGPath和CGColor的类型检查,下面是该错误的片段,演示了该行为.

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

这就是我想要做的

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文档说CFTypeID可以更改,因此不应依赖.

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