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
923 views
in Technique[技术] by (71.8m points)

swift - Binding an element of an array of an ObservableObject : 'subscript(_:)' is deprecated

I'm using an ObservableObject 'DataStore', which contains an array ('exampleList') of objects ('exampleObject').

@Published exampleList = [exampleObject]()

I'm calling the DataStore via @EnvironmentObject ('dataStore').

@EnvironmentObject var dataStore = DataStore()

Then I iterate the list with

ForEach(0..<dataStore.exampleList.count) { index in ....

To bind element of item to a detail view, I'm doing like this:

DetailView(itemBinding: $dataStore.exampleList[index])

Until Xcode11 beta 4, it worked perfectly. Since XCode11 beta 5, it still works but Xcode gives me this alert:

'subscript(_:)' is deprecated: See Release Notes for a migration path

I tried with simpler stuff, with a simple @State var containing an array of strings, and it's the same issue: when calling an element of this array, and trying to use the value into a TextField:

TextField("test", text: $test[0])

I get the same alert.

I don't understand how to fix it. Does that mean that we no longer can bind values inside an array? Then, how can we iterate an array and bind a specific item?

This is my first question on Stack Overflow, I apologize if my question is clumsy... Thanks a lot for your answers, I'm using Stack Overflow for years, it's amazing, I always find existing and helpful answers, but it is the first time I can't find any, that's why I'm asking.

See Question&Answers more detail:os

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

1 Reply

0 votes
by (71.8m points)

Xcode 11, beta 6 UPDATE:

Good news! Just as I suspected, in beta 6, the Binding conformance to MutableCollection has been been replaced with something else. Instead of conforming to MutableCollection, it now let your access the elements via @dynamicMemberLookup. The result is you now can keep doing $text[3] and no longer get a warning! It seems this question can be closed now.

Xcode 11, beta 5. Old answer:

I finally got some time to investigate this a little. As I mentioned in the comments, I think it would be wise to wait until the Collection conformance is completely removed (or replaced with something else). But just to satisfy our curiosity, I have created an extension on Binding, that I think does what the current Collection conformance does. The only difference is that, instead of accessing through a subscript, I implemented a function called element(_ idx: Int) to get a Binding<T> to the element.

If one day the conformance is completely removed, I may change the implementation, and conform to Collection myself. I cannot do it now, because it would conflict with the existent (and deprecated) implementation. For the time being, I think this demonstrate how to handle the warnings if you absolutely want to get rid of them.

Just to be clear. I am not using this code. As long as I can still access the elements through the subscript, I will still do it and ignore the warnings. This is just for academic purposes.

The extension is:

extension Binding where Value: MutableCollection, Value.Index == Int {
    func element(_ idx: Int) -> Binding<Value.Element> {
        return Binding<Value.Element>(
            get: {
                return self.wrappedValue[idx]
        }, set: { (value: Value.Element) -> () in
            self.wrappedValue[idx] = value
        })
    }
}

And it can be used like this:

struct MainView: View {
    @Binding var text: [String]

    var body: some View {
        TextField("", text: $text.element(0))
        TextField("", text: $text.element(1))
        TextField("", text: $text.element(2))
    }
}

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

...