[英]SwiftUI Initialzier requires String conform to Identifiable
I am attempting writing some SwiftUI code for the very first time and am running into an issue while trying to make a simple table view.我第一次尝试编写一些 SwiftUI 代码,并且在尝试制作简单的表格视图时遇到了问题。 In this code teams
is an array of String
, but the List
is giving me the following error: Initializer 'init(_:rowContent:)' requires that 'String' conform to 'Identifiable'
在此代码中, teams
是一个String
数组,但List
给了我以下错误: Initializer 'init(_:rowContent:)' requires that 'String' conform to 'Identifiable'
var body: some View {
let teams = getTeams()
Text("Hello there")
List(teams) { team in
}
}
Anyone have an idea what it means?有人知道这意味着什么吗?
The argument of List
is required to be unique, that's the purpose of the Identifiable
protocol List
的参数必须是唯一的,这就是Identifiable
协议的目的
You can do你可以做
List(teams, id: \.self) { team in
but then you are responsible to ensure that the strings are unique.但是你有责任确保字符串是唯一的。 If not you will get unexpected behavior.如果不是,你会得到意想不到的行为。
Better is to use a custom struct with an unique id
conforming to Identifiable
更好的是使用具有符合Identifiable
的唯一id
的自定义结构
I'm assuming that getTeams()
returns a [String]
(aka an Array<String>
).我假设getTeams()
返回一个[String]
(又名Array<String>
)。
The issue, as the error suggests, are the strings aren't identifiable.正如错误所暗示的那样,问题在于字符串无法识别。 But not just in the "they don't conform to the Identifiable
protocol" sense, but in the broader sense: if two strings are "foo"
, one is indistinguishable from the other;但不仅仅是在“它们不符合Identifiable
协议”的意义上,而是在更广泛的意义上:如果两个字符串是"foo"
,则一个与另一个无法区分; they're identical.它们是相同的。
This is unacceptable for a List
view.这对于List
视图是不可接受的。 Imagine you had a list that contains "foo"
entries, and the users dragged to rearrange one of them.想象一下,您有一个包含"foo"
条目的列表,并且用户拖动以重新排列其中一个。 How would List
be able to tell them apart, to know which of the two should move? List
如何区分它们,知道两者中的哪一个应该移动?
To get around this, you need to use an alternate source of identity to allow the List
to distinguish all of its entries.为了解决这个问题,您需要使用另一个身份来源来允许List
区分其所有条目。
You should watch the "Demystify SwiftUI" talk from this year's WWDC .您应该观看今年 WWDC 上的“Demystify SwiftUI”演讲。 It goes into lots of detail on exactly this topic.它详细介绍了这个主题。
The Apple preferred way to do this is to add a String extension: Apple 首选的方法是添加一个字符串扩展:
extension String: Identifiable {
public typealias ID = Int
public var id: Int {
return hash
}
}
See this code sample for reference: https://github.com/apple/cloudkit-sample-queries/blob/main/Queries/ContentView.swift请参阅此代码示例以供参考: https://github.com/apple/cloudkit-sample-queries/blob/main/Queries/ContentView.swift
If your trying to go over a 'List' of items of type 'Struct' Change the Struct to use 'Identifiable':如果您尝试 go 在“结构”类型的项目的“列表”上更改结构以使用“可识别”:
my Struct:我的结构:
struct Post : Identifiable{
let id = UUID()
let title: String
}
my List:我的列表:
let posts = [
Post( title: "hellow"),
Post( title: "I am"),
Post( title: "doing"),
Post( title: "nothing")
]
then you can go over your List:那么你可以在你的列表中使用 go :
List(posts){post in
Text(post.title
}
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.