gpt4 book ai didi

xcode - 我无法让键值链 (KVC) 在 Swift 中用于复合点路径

转载 作者:行者123 更新时间:2023-11-28 13:18:41 27 4
gpt4 key购买 nike

考虑以下类,它可以在 Xcode Playground 中执行:

import Foundation
class MyClass : NSObject {

var stringProperty:String
var integerProperty:Int
var linkedInstance:MyClass!

init(String string:String, Int int:Int) {
stringProperty = string
integerProperty = int
}
}

以下行按预期工作。

var myInstance = MyClass(String: "xxx", Int: 32)
myInstance.valueForKey("stringProperty")
myInstance.setValue(2, forKey: "integerProperty")
myInstance.valueForKey("integerProperty")

var anotherInstance = MyClass(String: "yyy", Int: 33)
myInstance.linkedInstance = anotherInstance
myInstance.linkedInstance.integerProperty = 2

但是尝试通过复合路径使用 KVC 访问值,

myInstance.valueForKey("linkedInstance.integerProperty")

导致错误“执行被中断,原因是 SIGABRT 信号。”我没有让任何复合路径与 Swift 一起工作,但它们在 Objective-C 中工作得很好。这与可选项有关吗?返回简单路径的值。

最佳答案

一旦您将最后一行更改为

myInstance.valueForKeyPath("linkedInstance.integerProperty")

你的 Playground 会和你一起玩得更好。

教训是:不要混淆 valueForKey:valueForKeyPath:!表达式 valueForKey("linkedInstance.integerProperty") 在 Swift 或 Objective-C 中永远不会成功,因为"linkedInstance.integerProperty" 不是 a key - 这是一个关键路径。

关于xcode - 我无法让键值链 (KVC) 在 Swift 中用于复合点路径,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/27595223/

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