该类别不符合键staticTexts的键值编码标准 [英] Class is not key value coding-compliant for the key staticTexts

查看:71
本文介绍了该类别不符合键staticTexts的键值编码标准的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我遇到此错误: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只是为了让单元测试可以访问它,这可能是一个较差的权衡.

I have this error: XCTAssertTrue failed: throwing "[<XCElementSnapshot 0x7fea978b1a10> valueForUndefinedKey:]: this class is not key value coding-compliant for the key staticTexts."

Here is the code:

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

Error is thrown on the second line.

I have looked at other answers on SO with the same error, and it's mostly caused by having a variable of a different class, however I don't see the possibility for this error here. Also, I checked to see that the predicate is formatted correctly.

How can I get rid of this error?

解决方案

Make sure that your staticTexts property is dynamic or otherwise available to objc (by marking it @objc for instance). Swift will not generate KVC-compliant accessors unless it thinks it needs to.

Alternately, use something other than NSPredicate here. Making a property dynamic when it's not needed has a performance cost, which is why Swift doesn't do it automatically. So marking it dynamic just so a unit test can access it may be a poor tradeoff.

这篇关于该类别不符合键staticTexts的键值编码标准的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆