简体   繁体   English

SwiftUI 字符串符合 Identifiable 似乎不正确

[英]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 ,不要在ForEachForEach(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.问题是您的RandomAccessCollectionRange<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>匹配更具体的initRange<Int>参数,而不是匹配不太具体的initRandomAccessCollection


So, your choices are:所以,你的选择是:

  1. Use the third 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 
}
  1. Convert to 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.

相关问题 SwiftUI - 错误 - 初始化程序 &#39;init(_:rowContent:)&#39; 要求 &#39;CustomDataModel&#39; 符合 &#39;Identifiable&#39; - SwiftUI - Error - Initializer 'init(_:rowContent:)' requires that 'CustomDataModel' conform to 'Identifiable' 在 SwiftUI 中使数组符合可识别性 - Conforming an array to Identifiable in SwiftUI 如何使枚举符合 Swift 中的可识别协议? - How to conform an enumeration to Identifiable protocol in Swift? 在 SwiftUI 的列表视图中使用可识别的对象集 - Using Identifiable set of objects in List View in SwiftUI 如何符合 SwiftUI 中的 ButtonStyle 协议? - How to conform the ButtonStyle protocol in SwiftUI? VStack 显示错误“实例方法 'sheet(item:onDismiss:content:)' 要求 'Int' 符合 'Identifiable'” - VStack shows error "Instance method 'sheet(item:onDismiss:content:)' requires that 'Int' conform to 'Identifiable'" 当字符串不符合错误时抛出的函数的 do catch 块时,如何获得正确的错误? - How to get the correct error when hitting a do catch block for a function that throws when String does not conform to Error? SwiftUI:GMSMapView 不符合协议 UIViewRepresentable - SwiftUI: GMSMapView does not conform to protocol UIViewRepresentable SwiftUI TabView 似乎不尊重@SceneStorage - SwiftUI TabView seems to not respect @SceneStorage SwiftUI 文本似乎依赖于图像 - SwiftUI Text seems dependant on Image
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM