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