Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
820 views
in Technique[技术] by (71.8m points)

ios - Class is not key value coding-compliant for the key staticTexts

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?

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

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.


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...