-1

I have been trying to create a "saved features" tab for my app. I embedded a button into my table view cell (which had a star icon - FavouriteStatusButton). When selected it would append the feature title (the name of the cell) to FavouriteFeatureDataBase.

@IBOutlet weak var FeatureTitle: UILabel!

 var array1: [String] = []
    
    var FavouriteFeatureDataBase = UserDefaults.standard

   
    @IBAction func FavouriteStatusButton(_ sender: UIButton) {

                array1.append(FeatureTitle.text!)
                FavouriteFeatureDataBase.set(array1, forKey: "ArrayOfFilteredFeatures")

...

It only seems to do this one cell at a time - For example I select 4 cells,

1: Name1, 2: Name2, 3: Name3 and 4: Name4

Instead of getting ["Name1", "Name2", "Name3", "Name4"], I'll only get ["Name4"].

I suspect this is because it is in the UITableViewCell but I'm not sure.

5
  • 1
    You start with an empty array, you add one element, you store that array. It will only have one element. It looks like you are trying to use UserDefaults when you should be using a more appropriate data store, such as Core Data
    – Paulw11
    Commented Feb 7 at 11:51
  • As @Paulw11 said, the array is initially empty - you need to retrieve the old data first before you save. Commented Feb 7 at 13:17
  • @Paulw11 I'm using UserDefaults because honestly I don't think I need core data for something this simple. Would it be possible to have advice on how to fix " It will only have one element" Commented Feb 8 at 8:44
  • @FrankieBaron and how do we do that? Commented Feb 8 at 8:44
  • You need to fetch the existing array from defaults, add the new element to that and then store the array in user defaults
    – Paulw11
    Commented Feb 8 at 20:20

1 Answer 1

0

As mentioned in the comments, you need to retrieve the data first.

You are storing an array - in order to retrieve it, you can use the array(forKey:) method of UserDefaults:

if let savedArray = favouriteFeatureDataBase.array(forKey: "ArrayOfFilteredFeatures") as? [String] {
    array1 = savedArray
}

You should call it somewhere before you append anything to your array, ideally in viewDidLoad() or viewWillAppear(_:):

@IBOutlet weak var featureTitle: UILabel!

var array1: [String] = []
var favouriteFeatureDataBase = UserDefaults.standard

@IBAction func favouriteStatusButton(_ sender: UIButton) {
    if let titleText = featureTitle.text {
        array1.append(titleText)
        favouriteFeatureDataBase.set(array1, forKey: "ArrayOfFilteredFeatures")
    }
}

override func viewDidLoad() {
    super.viewDidLoad()
    
    // Load the saved favourite features when the view is loaded
    loadFavouriteFeatures()
}

func loadFavouriteFeatures() {
    if let savedArray = favouriteFeatureDataBase.array(forKey: "ArrayOfFilteredFeatures") as? [String] {
        array1 = savedArray
    }
}

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.