在这里,我使用 Quick 对代码的一部分进行单元测试,我声明了容器变量以使其在weaklyScpoed 闭包之外保持事件状态。然后我收到警告...
describe("foo") {
it("bar") {
// warning: variable "container" was written to, but never read
var container: NSFPrioritizedDelegateContainer?
let result = weaklyScoped(NSObject()) {
container = NSFPrioritizedDelegateContainer.init(delegates: [$0], weakRef: true)
}
expect(result).to(beNil())
}
}
“weaklyScoped”函数也很简单
func weaklyScoped<T: AnyObject>(_ v: @autoclosure () -> T, action: (T) throws -> ()) rethrows -> T? {
weak var weakValue: T?
do { // optionally autoreleasepool
let value = v()
try action(value)
weakValue = value
}
return weakValue
}
如何删除警告并使容器在weaklyScoped 闭包之外保持事件状态?
最佳答案
说
_ = container
这样你就可以无害地读取它的值。
关于swift - 变量仅用于在单元测试中保持实例处于事件状态,并收到 "only written to but never read"警告,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58404722/