gpt4 book ai didi

swift - 未调用 keyValueObservingExpectationForObject 处理程序 block

转载 作者:搜寻专家 更新时间:2023-11-01 07:26:18 25 4
gpt4 key购买 nike

我在 Swift 中有一个测试用例试图等待属性更改:

    import XCTest

class AsynchronyousKVOTests: XCTestCase {

let testedObject : TestedObjet = TestedObjet.init()

func testKeyValueObservingExpectationForObject() {

// 1st approach, fails:
keyValueObservingExpectationForObject(self.testedObject, keyPath: "testedProperty") { object, changes in

return true // Breakpoint never reached!
}

// 2nd approach, also fails:
keyValueObservingExpectationForObject(self.testedObject, keyPath: "testedProperty", expectedValue: "After")

self.testedObject.updateTestedPropertyAsynchronyously("After")
// Expectation not fullfilled
waitForExpectationsWithTimeout(2, handler: nil)
}
}

class TestedObjet : NSObject {

var testedProperty : NSString = "Before"

func updateTestedPropertyAsynchronyously(value: NSString) {

dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), {
sleep(1)
dispatch_sync(dispatch_get_main_queue(), {
self.testedProperty = value
})
})
}

}

为什么从未调用 testKeyValueObservingExpectationForObject 处理程序 block ?

enter image description here

最佳答案

您必须使用 dynamic 关键字在 Swift 中启用键值观察:

dynamic var testedProperty: NSString = "Before"

关于swift - 未调用 keyValueObservingExpectationForObject 处理程序 block ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35912644/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com