【发布时间】:2017-01-06 05:09:48
【问题描述】:
这是一个相当短的问题,但我对如何解决它有点困惑。
for item in filteredAndSortedDates {
print(item.datesSectionHeader()) // Returns a value
print(item.value(forKeyPath: "datesSectionHeader") as Any) // Returns nil
// The "as Any" part up above is just to keep the compiler quiet. It doesn't have any meaning as this is just for testing purposes.
}
我对为什么会发生这种情况有点困惑。当上面返回一个值时,valueForKeyPath 怎么会返回nil?我打电话给NSDictionary。
这是我得到的日志:
HAPPENING THIS WEEK
nil
HAPPENING THIS WEEK
nil
HAPPENING THIS WEEK
nil
HAPPENING WITHIN A YEAR
nil
这是我声明datesSectionHeader的方式:
extension NSDictionary {
// FIXME
func datesSectionHeader() -> String {
// Doing some work in here.
}
}
【问题讨论】:
-
在您的问题中打印您的日志
-
当然!让我现在添加它。
-
print(item.value(forKeyPath: "datesSectionHeader") as NSString)
-
NSDictionary覆盖valueForKey:(Swift:value(forKey:)) 以返回其键的内容值,并且不检查键的 getter 方法。 (value(forKeyPath:)内部调用value(forKey:)。)据我测试,这种行为在 Objective-C 中也有,所以它不是 Swift 的错误。你需要找到解决办法。您可能需要使用一个以符合 KVC 的方式定义datesSectionHeader的类来包装字典。 -
作为@OOPer 所说的一个皱纹,如果你在一个键前面加上“@”,那么它会访问字典的属性而不是它的内容。所以,
item.value(forKeyPath: "@datesSectionHeader")会起作用。
标签: objective-c swift key-value-coding