[英]SwiftUI String conform Identifiable seems not correct
I am using an array in a List
, in the List
there's a ForEach
, like:我在
List
使用一个数组,在List
有一个ForEach
,例如:
struct AView: View {
@State var foo: [String] = ["a", "b", "c"]
var body: some View {
ZStack {
Color.white
List {
ForEach(foo.indices) { index in
Text(foo[index])
}
}
}
}
}
This works well, then I want to add a button to insert new items:这很好用,然后我想添加一个按钮来插入新项目:
List {
ForEach(foo.indices) { index in
Text(foo[index])
}
}
Button("Add") {
foo.append("foo")
}
}
Then I got the error, which is obviously:然后我得到了错误,这显然是:
ForEach<Range<Int>, Int, Text> 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`!
Here mentioned这里提到
Identifiable
or use ForEach(_:id:content:)
Identifiable
或使用ForEach(_:id:content:)
I can use ForEach(foo.indices, id:\\.self)
which solved the issue.我可以使用
ForEach(foo.indices, id:\\.self)
来解决这个问题。
I also want to try Identifiable
, and don't use id:\\.self
in the ForEach
, ForEach(foo.indices)
.我也想尝试
Identifiable
,不要在ForEach
、 ForEach(foo.indices)
使用id:\\.self
。
I added extension to String like:我为 String 添加了扩展名,例如:
extension String: Identifiable {
public var id: String { self }
}
But still got the same issue.但还是遇到了同样的问题。 Any thing I miss-understood?
有什么我想念的吗? thanks!
谢谢!
EDIT编辑
According to the comment @New Dev, as I literal indices
, so I added extension to Int
:根据评论@New Dev,因为我是字面
indices
,所以我添加了对Int
扩展:
extension Int: Identifiable {
public var id: Int { self }
}
still doesn't work.仍然不起作用。
ForEach
has multiple init
overloads. ForEach
有多个init
重载。
init(Range<Int>, content: (Int) -> Content)
only works with a constant range - hence the error. init(Range<Int>, content: (Int) -> Content)
仅适用于恒定范围 - 因此出现错误。
init(Data, content: (Data.Element) -> Content)
requires Data
to conform to RandomAccessCollection
of Identifiable
elements. init(Data, content: (Data.Element) -> Content)
要求Data
符合可Identifiable
元素的RandomAccessCollection
。 That's the one you want to use.这就是你想要使用的。
The problem is that your RandomAccessCollection
(to which Range<Int>
conforms) is a collection of Int
elements.问题是您的
RandomAccessCollection
( Range<Int>
符合的)是Int
元素的集合。
Though you can conform Int
to Identifiable
, this would still not work.尽管您可以使
Int
符合Identifiable
,但这仍然不起作用。 It would still use the first ForEach.init
with a Range<Int>
parameter, because of method overload preference - ie Range<Int>
matches the more specific init
with Range<Int>
parameter, rather than the less specific init
with RandomAccessCollection
.它仍然会使用带有
Range<Int>
参数的第一个ForEach.init
,因为方法重载偏好——即Range<Int>
匹配更具体的init
和Range<Int>
参数,而不是匹配不太具体的init
和RandomAccessCollection
。
So, your choices are:所以,你的选择是:
init(Data, id: KeyPath<Data.Element, ID>, content: (Data.Element) -> Content)
by explicitly specifying the id
.id
使用第三个init(Data, id: KeyPath<Data.Element, ID>, content: (Data.Element) -> Content)
。ForEach(foo.indices, id:\.self) { index in
}
Array<Int>
and conform Int: Identifiable
:Array<Int>
并符合Int: Identifiable
:extension Int: Identifiable { var id: Self { self } }
ForEach(Array(foo.indices)) { index in
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.