简体   繁体   English

SwiftUI Initialzier 要求 String 符合 Identifiable

[英]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.

相关问题 SwiftUI 字符串符合 Identifiable 似乎不正确 - SwiftUI String conform Identifiable seems not correct SwiftUI - 错误 - 初始化程序 &#39;init(_:rowContent:)&#39; 要求 &#39;CustomDataModel&#39; 符合 &#39;Identifiable&#39; - SwiftUI - Error - Initializer 'init(_:rowContent:)' requires that 'CustomDataModel' conform to 'Identifiable' swiftUI 选择器“通用结构‘选择器’要求‘字符串’符合‘视图’” - swiftUI picker “Generic struct 'Picker' requires that 'String' conform to 'View'” 初始化程序 'init(_:)' 要求 'Binding<string> ' 符合 'StringProtocol' SwiftUI 文本</string> - Initializer 'init(_:)' requires that 'Binding<String>' conform to 'StringProtocol' SwiftUI Text 要求“moveView”符合 swiftui 中的“StringProtocol” - requires that 'moveView' conform to 'StringProtocol' in swiftui VStack 显示错误“实例方法 'sheet(item:onDismiss:content:)' 要求 'Int' 符合 'Identifiable'” - VStack shows error "Instance method 'sheet(item:onDismiss:content:)' requires that 'Int' conform to 'Identifiable'" 任何 Identifiable 都不能符合“Identifiable” - any Identifiable can't conform to 'Identifiable' 如何使我的自定义 ViewModifier 仅适用于 SwiftUI 中符合(可识别和视图)的内容/视图? - How can I make my custom ViewModifier works only on Contents/Views that conform (Identifiable and View) in SwiftUI? SwiftUI - 如何从可识别结构中读取字符串数组 - SwiftUI - How do I read a string array out of a Identifiable struct 运算符函数“&gt;”要求“UserRating”符合“BinaryInteger” - SwiftUI ObservableObject - Operator function '>' requires that 'UserRating' conform to 'BinaryInteger' - SwiftUI ObservableObject
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM