UPGRADE YOUR SKILLS: Learn advanced Swift and SwiftUI on Hacking with Swift+! >>

Creating a binding property from a computed property

Forums > SwiftUI

I have an array passed to a SwiftUI view.

var someArray: [Something]

I create another array with some items filtered from it:

var filteredArray: [String] {
  // do  some actions on someArray
  return 
}

(the computed property).

I need to display the results of filteredArray in a List.

The problem I'm having is how do I make the filteredArray Binding? I can't use State because it's a computed property, and if I don't use State, the changes I make in filteredArray won't appear in the UI.

I feel like something is fundamentally wrong with how I'm approaching this, but am not sure how to proceed.

3      

hi,

i approached this by making the List depend on a function of someArray, so it would clearly depend on someArray.

example: if i have a searchText String, and wish to filter the list by things that include the searchText in a name property:

ForEach(someArray.filter({ searchTextAppears(in: $0.name!) })) { item in
  // what ever you show here for this item
}

i use this function to not worry too much about spaces and such (thanks, Paul Hudson)

    func searchTextAppears(in name: String) -> Bool {
        let cleanedSearchText = searchText.trimmingCharacters(in: .whitespacesAndNewlines)
        if cleanedSearchText.isEmpty {
            return true
        }
        return name.localizedCaseInsensitiveContains(cleanedSearchText.lowercased())
    }

so, rather than naming a variable for the list of filtered items, i just use a function.

hope that helps,

DMG

4      

Can i ask why your need the filtered array as a binding? Is it used in conjunction with a search? And is that someArray being used for the filtered array?

4      

Can i ask why your need the filtered array as a binding?

Not sure I need it to be, so would be open to other options.

The filtered array is displayed in a List, and the user can reorder the rows.

I'm trying to keep track of which item is the first item in the List, and to colour that first item differently.

Setting the colour of filteredArray[0] is producing inconsistent results, and I'm guessing it's because it is not binding.

3      

Ok, so I think I solved this by:

  • Initializing an empty State variable:

@State var filteredArray = [String]()

  • Populating it in .onAppear:
.onAppear {
  self.filteredArray = getFilteredArray()
}

Thanks everyone for your help!

3      

Hacking with Swift is sponsored by Essential Developer

SPONSORED Join a FREE crash course for mid/senior iOS devs who want to achieve an expert level of technical and practical skills – it’s the fast track to being a complete senior developer! Hurry up because it'll be available only until April 28th.

Click to save your free spot now

Sponsor Hacking with Swift and reach the world's largest Swift community!

Archived topic

This topic has been closed due to inactivity, so you can't reply. Please create a new topic if you need to.

All interactions here are governed by our code of conduct.

 
Unknown user

You are not logged in

Log in or create account
 

Link copied to your pasteboard.