我有点困惑,因为什么/什么时候与expectationForNotification而不是expectingWithDescription.我无法在
swift中找到任何明确的示例,用于何时以及您使用此呼叫.
我假设它可能测试通知,但它看起来可能只是通知中心的整个addObserver()调用的更方便的包装.
有人可以简要解释一下它的作用,什么时候使用它,还有几行示例代码?
谢谢
正如您已经想象的期望值.通知是检查通知是否被提出的便利期望.
这个测试:
func testItShouldRaiseAPassNotificationV1() { let expectation = expectationWithDescription("Notification Raised") let sub = NSNotificationCenter.defaultCenter().addObserverForName("evPassed",object: nil,queue: nil) { (not) -> Void in expectation.fulfill() } NSNotificationCenter.defaultCenter().postNotificationName("evPassed",object: nil) waitForExpectationsWithTimeout(0.1,handler: nil) NSNotificationCenter.defaultCenter().removeObserver(sub) }
可以替换这个:
func testItShouldRaiseAPassNotificationV2() { expectationForNotification("evPassed",handler: nil) NSNotificationCenter.defaultCenter().postNotificationName("evPassed",handler: nil) }
你可以在这个Objc.io number中找到一个很好的解释.
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。