ios - 如何将可选类型绑定到非可选类型
问题描述
Xcode 说无法将类型 'Binding<String?>' 的值转换为预期的参数类型 'Binding<String>'
struct CustomContentView: View {
@State var queryItem = URLQueryItem.init(name: "key", value: nil)
var body: some View {
//queryItem.value is optional string
TextField.init("value", text: $queryItem.value)
//Cannot convert value of type 'Binding<String?>' to expected argument type 'Binding<String>'
}
}
解决方案
使用以下
var body: some View {
TextField("url", text: Binding(
get: { self.queryItem.value ?? ""},
set: { self.queryItem.value = $0 } )
)
}
推荐阅读
- python-3.x - 如何在 Mac m1 上使用自制软件安装 python 3.9.6
- python - 我怎样才能使这个 web3 python 脚本更快?
- debian - 无法使用 Snap 方法安装 certbot
- python - 熊猫正常运行时间到日期时间并重置为正常运行时间
- ruby-on-rails - 如何使硒网络驱动程序单击反应聚光灯内的按钮(React Joyride)?
- apache-superset - 如何在 apache 超集中显示/显示静态 HTML 文件?
- php - 为每个 PHP 排序数据 - Codeigniter
- javascript - 当 API 调用不是以“https://”开头时,为什么 axios 库无法发出请求?
- r - 当对应列等于1时如何赋值?
- python - 无法使用 selenium 和 chromedriver 获取此代码以在 pycahrm 中工作