传递给泛型函数时如何访问 @Published 属性 的包装值
How to access the wrapped value of a @Published property when passing to generic function
当我将声明为 @Published
的 属性 传递给通用函数时,我收到一个引用已发布值的错误。在我使用变量的其他任何地方都很好,只是在传递给通用函数时不行。
错误:
error: cannot convert value 'user' of type 'User?' to expected type 'Published<User?>.Publisher?', use wrapper instead
这是游乐场代码:
struct User {}
class Test {
@Published var user: User?
}
func normalFunc(_ argument: User?) -> User? {
return argument
}
func genericFunc<T>(_ argument: T?) -> T? {
return argument
}
let test = Test()
normalFunc(test.user) // Ok
genericFunc(test.user) // Error
我能做的最好的事情就是将值括在括号中,如下所示:
genericFunc((test.user)) // Ok!
这是一个已确认的错误,已通过 https://github.com/apple/swift/pull/30129
修复
当我将声明为 @Published
的 属性 传递给通用函数时,我收到一个引用已发布值的错误。在我使用变量的其他任何地方都很好,只是在传递给通用函数时不行。
错误:
error: cannot convert value 'user' of type 'User?' to expected type 'Published<User?>.Publisher?', use wrapper instead
这是游乐场代码:
struct User {}
class Test {
@Published var user: User?
}
func normalFunc(_ argument: User?) -> User? {
return argument
}
func genericFunc<T>(_ argument: T?) -> T? {
return argument
}
let test = Test()
normalFunc(test.user) // Ok
genericFunc(test.user) // Error
我能做的最好的事情就是将值括在括号中,如下所示:
genericFunc((test.user)) // Ok!
这是一个已确认的错误,已通过 https://github.com/apple/swift/pull/30129
修复