gpt4 book ai didi

swift - 变量仅用于在单元测试中保持实例处于事件状态,并收到 "only written to but never read"警告

转载 作者:行者123 更新时间:2023-11-30 10:34:21 25 4
gpt4 key购买 nike

在这里,我使用 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/

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