0

I need a code to write data to the board,

variables:

favoriteKey String "favoriteProducts1"
String productId "803566"


removeProductToFavorites (favoriteKey: "favoriteProducts1", productId: productsObjectArray [sender.tag] .code!)

@objc func favoriteBtnPressed1 (_ sender: UIButton) {
        let productStatus = checkProductFavoriteIsAvailable (favoriteKey: "favoriteProducts1", productId: productsObjectArray [sender.tag] .code!)
        if productStatus == true {
            removeProductToFavorites (favoriteKey: "favoriteProducts1", productId: productsObjectArray [sender.tag] .code!)
        } else {
            saveProductToFavorites (favoriteKey: "favoriteProducts1", productId: productsObjectArray [sender.tag] .code!)
        }
    }
    
    
func checkProductFavoriteIsAvailable (favoriteKey: String, productId: String) -> Bool {
        var status = false
        if let arr = UserDefaults.standard.array (forKey: favoriteKey) as? [String] {
            status = true
        }
        return status
    }
    
    func saveProductToFavorites (favoriteKey: String, productId: String) {
        var favoriteArray = UserDefaults.standard.array (forKey: favoriteKey) as? [String]
        favoriteArray? .append (productId)
        UserDefaults.standard.set (favoriteArray, forKey: productId)
    }
    
    func removeProductToFavorites (favoriteKey: String, productId: String) {
        
    }
    

I need to write an array in a UserDefaults file with a list of favorite products.

The above code is designed to: 1. checkProductFavoriteIsAvailable - this function is to return true - if the array already contains the product code, or false if it does not contain 2. saveProductToFavorites - save the new number (String) to the array and to the file 3. removing the selected product code from the array and file

Can I ask for help?

UPDATE

my debug: http://serwer1356363.home.pl/pub/debug.png

traff
  • 135
  • 5
  • What's wrong with your current code? – Larme Jul 09 '18 at 12:14
  • FUnction saveProductToFavorites - not working, checkProductFavoriteIsAvailable - I do not know if it works because I have no added value to the array, removeProductToFavorites - I do not know how to do it :( – traff Jul 09 '18 at 12:23

3 Answers3

0

checkProductFavoriteIsAvailable does not check whether product exists in the array. It checks whether the array with key favoriteKey exists in Userdefaults. You need to take the array from user defaults and check in that array whether the product exists.

Mohammad Sadiq
  • 5,070
  • 28
  • 29
  • how to save new product or remove old from array in userdefaults? – traff Jul 09 '18 at 13:07
  • what you are doing is setting and retrieving array. Thats right. But you need to add product in array before adding it to default. And you need to remove product from array. You can refer https://stackoverflow.com/questions/25179668/how-to-save-and-read-array-of-array-in-nsuserdefaults-in-swift – Mohammad Sadiq Jul 09 '18 at 13:12
0

I think what you wan to do in function 'saveProductToFavorites' is, whatever 'productId' you are getting, you want to add it in array stored against key, 'favoritekey' Change you code to following,

var favoriteArray = UserDefaults.standard.array(forKey: favoriteKey) as? [String]
if favoriteArray == nil {
    favoriteArray = []
}
favoriteArray.append(productId)
UserDefaults.standard.set(favoriteArray, forKey: favoriteKey)
nikBhosale
  • 531
  • 1
  • 5
  • 27
0

These are two functions to add a product ID to favorites and remove a product ID from favorites.

saveProductToFavorites checks first if there is an array in UserDefaults. If yes, it adds the ID if the array does not contain the ID. If no, it creates a new array. Finally the array is saved back

func saveProductToFavorites(favoriteKey: String, productId: String) {
    if var favoriteArray = UserDefaults.standard.stringArray(forKey: favoriteKey) {
        if !favoriteArray.contains(productId) {
            favoriteArray.append(productId)
            UserDefaults.standard.set(favoriteArray, forKey: favoriteKey)
        }
    } else {
        let favoriteArray = [productId]
        UserDefaults.standard.set(favoriteArray, forKey: favoriteKey)
    }
}

removeProductFromFavorites removes the ID if the array and the ID exists

func removeProductFromFavorites(favoriteKey: String, productId: String) {
    guard var favoriteArray = UserDefaults.standard.stringArray(forKey: favoriteKey),
        let index = favoriteArray.index(of: productId) else { return }
    favoriteArray.remove(at: index)
    UserDefaults.standard.set(favoriteArray, forKey: favoriteKey)
}
vadian
  • 274,689
  • 30
  • 353
  • 361