我有这个错误: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
属性是dynamic
或以其他方式可用于 objc(例如将其标记为 @objc
)。 Swift 不会生成符合 KVC 的访问器,除非它认为有必要。
或者,在这里使用 NSPredicate
以外的东西。在不需要时使属性动态化会降低性能,这就是 Swift 不会自动执行此操作的原因。因此,将其标记为 dynamic
以便单元测试可以访问它可能是一个糟糕的权衡。
关于ios - 类对于键 staticTexts 不符合键值编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/31323632/