Mda*_*ank 7 xcode ios swift swiftui observableobject
在我的应用程序中,我有一系列可以从另一个视图添加的朋友。我已将这组朋友存储在一个类中,该类是ObservableObject. 在我的主视图中FriendListView,我使用ForEach为用户添加的每个朋友显示自定义视图。
我无法添加到数组并让列表显示新项目。根据我能够理解和组合的内容,我尝试了两种不同的尝试。如果有人能够提供任何帮助,我们将不胜感激。我目前有显示第二个视图的方法,我将在其中添加注释掉的新朋友,并且有一种自动保存新朋友的方法作为导航栏项目存在。
如果在类初始值设定项期间将朋友列表附加到数组中,我就成功地显示了朋友列表。调用类方法从另一个视图添加到数组似乎是可行的,因为我能够打印数组的内容,但 UI 不会更新。
这是通过使用一种ForEach(0..<Array.count)方法完成的,但在添加新项目后我收到以下控制台错误。
错误
ForEach<Range<Int>, Int, HStack<TupleView<(Spacer, FriendView, Spacer)>>> count (4) != its initial count (3).
`ForEach(_:content:)` should only be used for *constant* data.
Instead conform data to `Identifiable` or use `ForEach(_:id:content:)` and provide an explicit `id`!
Run Code Online (Sandbox Code Playgroud)
型号代码
ForEach<Range<Int>, Int, HStack<TupleView<(Spacer, FriendView, Spacer)>>> count (4) != its initial count (3).
`ForEach(_:content:)` should only be used for *constant* data.
Instead conform data to `Identifiable` or use `ForEach(_:id:content:)` and provide an explicit `id`!
Run Code Online (Sandbox Code Playgroud)
用户界面代码
struct FriendListView: View {
@EnvironmentObject var dataModel: FriendModel
@State private var displayAddFriendSheet = false
var body: some View {
GeometryReader { geo in
NavigationView {
ScrollView {
ForEach(0..<self.dataModel.petList.count) { pet in
HStack {
Spacer()
FriendView(
name: self.dataModel.petList[pet].name,
breed: self.dataModel.petList[pet].breed,
screenWidth: geo.size.width,
randomPic: Int.random(in: 1...2)
)
// FriendView(
// name: self.dataModel.petList[pet].name,
// breed: self.dataModel.petList[pet].breed,
// screenWidth: geo.size.width)
Spacer()
}
}
}// End of ScrollView
.navigationBarTitle(Text("Furiends"))
.navigationBarItems(trailing: Button(action: self.showAddFriend) {
Image(systemName: "plus")
}
)
.sheet(isPresented: self.$displayAddFriendSheet) {
AddFriendView()
}
}// End of NavigationView
}// End of GeometryReader geo1
}// End of body
func showAddFriend() {
// self.displayAddFriendSheet.toggle()
self.dataModel.addFriend(name: "Jax", breed: "Doodle")
print(dataModel.petList)
}// Enf of showAddFriend
}// End of FriendListView
Run Code Online (Sandbox Code Playgroud)
在看到第一次尝试的错误后,我阅读了如何跳过范围并仅提供数组作为的一部分,因为我在结构中使用 UUIDForEach遵守协议。由于模型代码与我的第一次尝试相同,我更新的 UI 代码如下。请注意,唯一的更改是 ForEach,但我收到以下错误:IdentifiablePet
错误
Cannot convert value of type '[Pet]' to expected argument type 'Range<Int>'
用户界面代码
struct FriendListView: View {
@EnvironmentObject var dataModel: FriendModel
@State private var displayAddFriendSheet = false
var body: some View {
GeometryReader { geo in
NavigationView {
ScrollView {
ForEach(self.dataModel.petList) { pet in // <--- Changed to directly reference the array
HStack {
Spacer()
FriendView(
name: self.dataModel.petList[pet].name,
breed: self.dataModel.petList[pet].breed,
screenWidth: geo.size.width,
randomPic: Int.random(in: 1...2)
)
// FriendView(
// name: self.dataModel.petList[pet].name,
// breed: self.dataModel.petList[pet].breed,
// screenWidth: geo.size.width)
Spacer()
}
}
}// End of ScrollView
.navigationBarTitle(Text("Furiends"))
.navigationBarItems(trailing: Button(action: self.showAddFriend) {
Image(systemName: "plus")
}
)
.sheet(isPresented: self.$displayAddFriendSheet) {
AddFriendView()
}
}// End of NavigationView
}// End of GeometryReader geo1
}// End of body
func showAddFriend() {
// self.displayAddFriendSheet.toggle()
self.dataModel.addFriend(name: "Jax", breed: "Doodle")
print(dataModel.petList)
}// Enf of showAddFriend
}// End of FriendListView
Run Code Online (Sandbox Code Playgroud)
我假设通过订阅访问的剩余尝试会混淆编译器,因此只要Pet以下Identifiable内容应该有效
ForEach(self.dataModel.petList) { pet in
HStack {
Spacer()
FriendView(
name: pet.name, // << pet is not index
breed: pet.breed, // << so access by property
screenWidth: geo.size.width,
randomPic: Int.random(in: 1...2)
)
// .. other code
Run Code Online (Sandbox Code Playgroud)
| 归档时间: |
|
| 查看次数: |
1791 次 |
| 最近记录: |