【问题标题】:Class is not key value coding-compliant for the key staticTexts类与键 staticTexts 的键值编码不兼容
【发布时间】:2015-07-09 16:46:34
【问题描述】:

我有这个错误:XCTAssertTrue failed: throwing "[<XCElementSnapshot 0x7fea978b1a10> valueForUndefinedKey:]: this class is not key value coding-compliant for the key staticTexts."

代码如下:

let predicate = NSPredicate(format: "(self.staticTexts[%@].exists == true) AND (self.staticTexts[%@].exists == true)", message, nameString)
XCTAssert(app.collectionViews.childrenMatchingType(.Cell).elementMatchingPredicate(predicate).exists)

第二行抛出错误。

我已经查看了关于 SO 的其他答案,但出现了相同的错误,这主要是由具有不同类的变量引起的,但是我在这里看不到此错误的可能性。另外,我检查了谓词的格式是否正确。

我怎样才能摆脱这个错误?

【问题讨论】:

  • staticTexts是什么类型,父类是什么类型?符合键值编码的变量必须是属性或必须具有显式的 getter 和 setter 方法。

标签: ios swift nspredicate


【解决方案1】:

确保您的staticTexts 属性是dynamic 或其他可供objc 使用的(例如,将其标记为@objc)。除非它认为需要,否则 Swift 不会生成符合 KVC 的访问器。

或者,在此处使用 NSPredicate 以外的其他内容。在不需要时使属性动态化会降低性能,这就是 Swift 不会自动执行此操作的原因。因此,将其标记为 dynamic 以便单元测试可以访问它可能是一个糟糕的权衡。

【讨论】:

    【解决方案2】:

    显然,当我将谓词应用于静态文本与单元格然后尝试访问谓词内的静态文本时,错误就消失了。比如,

    let predicate = NSPredicate("self.title like %@", message)
    app.descendantsMatchingType(.StaticText).elementMatchingPredicate(predicate).exists
    

    将摆脱错误。

    【讨论】:

      猜你喜欢
      • 2013-10-11
      • 2013-05-23
      • 2017-09-27
      • 1970-01-01
      • 1970-01-01
      • 2015-08-27
      • 2017-01-23
      • 2011-03-17
      • 2012-09-24
      相关资源
      最近更新 更多