Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/19.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 使用数组和UserDefaults的操作_Ios_Swift - Fatal编程技术网

Ios 使用数组和UserDefaults的操作

Ios 使用数组和UserDefaults的操作,ios,swift,Ios,Swift,我需要一个代码将数据写入电路板 variables: favoriteKey String "favoriteProducts1" String productId "803566" removeProductToFavorites (favoriteKey: "favoriteProducts1", productId: productsObjectArray [sender.tag] .code!) @objc func favoriteBtnPressed1 (_ sender:

我需要一个代码将数据写入电路板

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) {
        
    }
    
我需要在UserDefaults文件中编写一个数组,其中包含最喜欢的产品列表

上述代码旨在: 1.CheckProductFavoriteAvailable-如果数组已包含产品代码,则此函数将返回true;如果数组不包含产品代码,则返回false 2.saveProductToFavorites-将新数字(字符串)保存到数组和文件中 3.从阵列和文件中删除选定的产品代码

我能寻求帮助吗

更新


我的调试:

检查产品收藏夹是否可用
不检查阵列中是否存在产品。它检查
Userdefaults
中是否存在键为
favoriteKey
的数组。
您需要从用户默认设置中获取数组,并在该数组中检查产品是否存在

我认为在函数“saveProductToFavorites”中要做的是,无论您得到的是什么“productId”,都要将其添加到存储在“favoritekey”项下的数组中 将您的代码更改为:

var favoriteArray = UserDefaults.standard.array(forKey: favoriteKey) as? [String]
if favoriteArray == nil {
    favoriteArray = []
}
favoriteArray.append(productId)
UserDefaults.standard.set(favoriteArray, forKey: favoriteKey)

这两个函数用于将产品ID添加到收藏夹和从收藏夹中删除产品ID

saveProductToFavorites
首先检查
UserDefaults
中是否有数组。如果是,则在数组不包含ID的情况下添加ID。如果否,则创建一个新数组。最后,数组被保存回来

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
如果数组和ID存在,则删除ID

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)
}

您当前的代码有什么问题?函数saveProductToFavorites-不起作用,请检查ProductFavorite是否可用-我不知道它是否起作用,因为我对数组没有附加值,请删除ProductToFavorites-我不知道如何做:(如何在userdefaults中保存新产品或从数组中删除旧产品?您要做的是设置和检索数组。没错。但在将产品添加到默认值之前,您需要在数组中添加产品。并且您需要从数组中删除产品。您可以参考