gpt4 book ai didi

ios - 无法在 TextField 中创建绑定(bind) - 范围内的 'cannot find $social''

转载 作者:行者123 更新时间:2023-12-05 02:47:07 25 4
gpt4 key购买 nike

我无法使用我的“用户名”属性创建绑定(bind) - Xcode 给我错误“无法在范围内找到 $social”。以下是一些基本代码:

我有问题的观点:

struct ProfileSetter: View {
@Binding var profile: Profile

var body: some View {
ForEach(profile.socials, id: \.id) { social in
TextField(social.medium.rawValue, text: $social.username) //-> cannot find $social in scope
}
}
}

它的父级:

struct ProfileView: View {
@StateObject private var viewModel = ViewModel()

var body: some View {
ProfileSetter(profile: $viewModel.myProfile)
}
}

简化的 View 模型:

class ViewModel: ObservableObject {
@Published var myProfile = Profile(socials: [//multiple instances of Social])
}

最后,模型:

struct Profile {
// other properties
var socials: [Social]
}

struct Social {
// other properties
var username: String
}

将文本字段替换为 Text(social.username) 效果很好,因此创建绑定(bind)似乎是问题所在。

最佳答案

你不能直接绑定(bind)到值,你应该通过父对象来绑定(bind),比如

ForEach(profile.socials.indices, id: \.self) { index in
TextField(profile.socials[index].medium.rawValue,
text: $profile.socials[index].username)
}

关于ios - 无法在 TextField 中创建绑定(bind) - 范围内的 'cannot find $social'',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/65186044/

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