gpt4 book ai didi

ios - 我如何输入检查 CGColor/CGPath?

转载 作者:可可西里 更新时间:2023-11-01 00:59:08 25 4
gpt4 key购买 nike

所以看起来有一个 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 用作长期解决方案

最佳答案

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

这意味着您应该在运行时获取类型 ID,在您的情况下:

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

关于ios - 我如何输入检查 CGColor/CGPath?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/38252330/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com