问题
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?
回答1:
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.
回答2:
Apparently, the error goes away when I apply the predicate to static texts vs. the cells and then try to access the static texts inside the predicate. So for example,
let predicate = NSPredicate("self.title like %@", message)
app.descendantsMatchingType(.StaticText).elementMatchingPredicate(predicate).exists
would get rid of the error.
来源:https://stackoverflow.com/questions/31323632/class-is-not-key-value-coding-compliant-for-the-key-statictexts