Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
429 views
in Technique[技术] by (71.8m points)

swiftui - 不建议使用SwiftUI ForEach'identified(by :)'。 使用ForEach(_:id :)或List(_:id :)(SwiftUI ForEach 'identified(by:)' is deprecated. Use ForEach(_:id:) or List(_:id:))

On XCode 11 beta 4 the following seems to be deprecated and I don't know how to rewrite this.

(在XCode 11 beta 4上,以下内容似乎已被弃用,我不知道该如何重写。)

Does anybody know how to use ForEach(_:id:) ?

(有人知道如何使用ForEach(_:id:)吗?)

@State private var showTargets = [
    (id: 1, state: false, x: 109.28, y: 109.28),
    (id: 2, state: false, x: 683, y: 109.28),
    (id: 3, state: false, x: 1256.72, y: 109.28)
]

...

(...)

var body: some View {
    HStack {

        ForEach(showTargets.identified(by: .id)) { item in
            Text(String(item.x))

        }
}
  ask by krjw translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

(Still working with Xcode 11.0 / Swift 5.1)

((仍可使用Xcode 11.0 / Swift 5.1))

I haven't downloaded Xcode Beta 4 yet, but according to the documentation , it should be something like:

(我尚未下载Xcode Beta 4,但是根据文档 ,它应该类似于:)

ForEach(showTargets, id: .id) { item in
    Text(String(item.x))
}

You can also use a struct that conforms to Identifiable (note that this won't work on tuple because you can't add protocol conformance):

(您还可以使用符合Identifiablestruct (请注意,这不适用于元组,因为您无法添加协议一致性):)

struct Targets: Identifiable {
    var id: Int
    var state: Bool
    var x: Double
    var y: Double
}

let showTargets = [
    Targets(id: 1, state: false, x: 109.28, y: 109.28),
    Targets(id: 2, state: false, x: 683, y: 109.28),
    Targets(id: 3, state: false, x: 1256.72, y: 109.28)
]

ForEach(showTargets) { item in
    Text(String(item.x))
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...