Swift数组附加不附加值,而是替换它 [英] Swift Array append not appending values but replacing it

查看:37
本文介绍了Swift数组附加不附加值,而是替换它的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试将一些字典添加到数组中,并使用 UserDefaults 保存它,但是问题是数组没有追加新字典,而是仅替换了字典.我该如何解决这个问题?

I am trying to add some dictionaries into an array and save it using UserDefaults, but the problem is the array doesn't append new dictionary but only replaces the dictionary. How can I fix this issue?

下面是代码:

class Bookmark: NSObject {

     var bookmark: [[String:Any]] = []

    override init() {
        super.init()
    }



    func setBookmark(imageURL:String, title:String, description:String, summary:String, date:String, link:String)  {
        bookmark.append(["imageURL":imageURL , "title":title , "description":description, "summary":summary , "date":date, "link":link])
        UserDefaults.standard.set(bookmark, forKey: "bookmark")

    }


    func getBookrmark() -> [Any] {
        let loadedBookmark = UserDefaults.standard.array(forKey: "bookmark")
        return (loadedBookmark)!
    }

}

使用书签类:

class ReadViewController: UIViewController  {

    var bookmark = Bookmark()


       func save() {
            bookmark.setBookmark(imageURL: nImageURL.absoluteString, title: ntitle!, description: nDescription.htmlToString , summary: nSummary!, date: nDate!, link: nLink.absoluteString)
        }

推荐答案

没有足够的信息,但是我可以猜到您在哪里犯了错误.

There is not enough information but I can guess you where you have made mistake.

您的类 Bookmark 不是 singleton 类,因此,每次 Bookmark()每次都创建一个新实例.这意味着它将为每个实例创建一个新的 bookmark 对象.

Your class Bookmark is not singleton class so,every time Bookmark() create new instance every time. that means it will create new bookmark object for every instance.

我建议您使用func func setBookmark(imageURL:String,title:String,description:String,summary:String,date:String,link:String)

What I suggest you is inside func func setBookmark(imageURL:String, title:String, description:String, summary:String, date:String, link:String)

方法1::在单独的变量中获取最新的书签,并在其中添加新对象,并写入User Default并更新全局对象

Method 1 : Fetch the latest bookmarks in separate variable and append new object inside it and write to User Default as well as update the global object

方法2 ,或者您可以将其设置为单例并在每次执行操作时使用共享实例.

Method 2 Or you can make it singleton and use shared instance for every time you perform operation.

方法3 的另一种解决方案是在 AppDelegate 或您的singleton类中创建书签的全局对象,然后使用该对象

Method 3 Another solution can be create global object of Bookmark in AppDelegate or Your singleton class and then use that object

但是

func setBookmark(imageURL:String, title:String, description:String, summary:String, date:String, link:String)  {
        bookmark.append(["imageURL":imageURL , "title":title , "description":description, "summary":summary , "date":date, "link":link])
        UserDefaults.standard.set(bookmark, forKey: "bookmark")

  }

这是不正确的做法.您直接用新值替换用户默认值,但是您的意图是添加新元素,而不是替换用户默认值中的现有数据.

This is the bad practice to follow. You are directly replacing User default with new value however your intension is to append new element not replace existing data in userdefault.

您应该始终获取最新信息并进行更新,就像方法1所示,以及如果要在userdefault中获取数据,则不使用全局对象 var书签:[[String:Any]] = [] ,因为您已经在其中 getBookrmark 方法

You should always fetch latest and update it Like method 1 show as well as If you wanted to fetch the data in userdefault there is no use of your global object var bookmark: [[String:Any]] = [] because you have already getBookrmark method there

希望您很清楚

这篇关于Swift数组附加不附加值,而是替换它的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆