简体   繁体   English

如何保存附加到全局数组swift 2的数据

[英]How to persist data that is appended to a global array swift 2

Before I start, just wanted to say I'm very new to app development in general, I've only been at this for a month, so feel free to dumb it down to me as much as possible haha. 在我开始之前,只是想说我对应用程序开发一般都是新手,我只是在这一个月里,所以尽可能地把它愚蠢地告诉我哈哈。

Ok, so I'm working on a quote app and so I've created an array that I can access from any view controller. 好的,所以我正在开发一个引用应用程序,所以我创建了一个可以从任何视图控制器访问的数组。 This that will contain "liked" quotes, which is added from another another view. 这将包含“喜欢”的引号,这是从另一个视图添加的。

Here is my global "likedArray". 这是我的全球“likesArray”。 It resides in its own swift file. 它驻留在自己的swift文件中。

import Foundation

struct Globals {

    static var likedArray: [String] = ["Touch 'Liked' To Continue..."]

}

Quotes are added to likedArray by this method, from another view controller file. 通过此方法从另一个视图控制器文件将引号添加到likesArray。

@IBAction func likedbuttonPressed(sender: AnyObject) {

    Globals.likedArray.append(quoteLabel.text)
}

And "liked" quotes are shown in another view via this method 并且通过此方法在另一个视图中显示“喜欢”的引号

// Like button method to move forward in array
@IBAction func likedButtonTouched(sender: AnyObject) {

    self.favouritesLabel.fadeOut(completion: {
        (finished: Bool) -> Void in

    self.currentlikedArrayIndex++
    if self.currentlikedArrayIndex < Globals.likedArray.count {
        self.favouritesLabel.text = Globals.likedArray[self.currentlikedArrayIndex]
    } else {
        self.currentlikedArrayIndex--
    }

     self.favouritesLabel.fadeIn()

    })  
}

And this all works fine, except that when I quit the app, all liked quotes are gone. 这一切都很好,除了当我退出应用程序时,所有喜欢的引用都消失了。

So, my question is how do I save this data? 所以,我的问题是如何保存这些数据? If you need any more information about my code, please let me know. 如果您需要有关我的代码的更多信息,请告诉我。 Thanks in advance. 提前致谢。

The most hassle free way is probably to use NSUserDefaults , can follow this tutorial to find out how exactly 最麻烦的方式可能是使用NSUserDefaults ,可以按照本教程找出具体的方法

the jist of the tutorial: 教程的主旨:

//for writing to storage
let defaults = NSUserDefaults.standardUserDefaults()
let array = ["Hello", "World"]
defaults.setObject(array, forKey: "SavedArray")

//for reading
let array = defaults.objectForKey("SavedArray") as? [String] ?? [String]()

So basically, you should make a setter method for your global array so each time the array is set, it will write to the NSUserDefaults, then on app launch it should populate the array with whats in the NSUserDefaults 所以基本上,你应该为你的全局数组创建一个setter方法,所以每次设置数组时,它都会写入NSUserDefaults,然后在应用程序启动时它应该在NSUserDefaults中填充数组中的whats

update: (just did this off the top of my head) 更新:(刚刚做到这一点)

struct Globals {

    static var likedArray: [String] = ["Touch 'Liked' To Continue..."]

    func addLikedString(likedString: String) {

        self.likedArray.append(likedString)
        NSUserDefaults.standardUserDefaults().setObject(self.likedArray, forKey: "SavedArray")
    }

    func getLikedStringAtIndex(index:Int) -> Int {
        return self.likedArray[index] 
    }

}

//inside your app delegate, or somewhere appropriate, to load the array when the app starts

Globals.likedArray = NSUserDefaults.standardUserDefaults().objectForKey("SavedArray") as? [String] ?? [String]()

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何确认来自 Firestore 的数据附加在 swift 中? - How to confirm the data from Firestore is appended in swift? Swift Array未附加到 - Swift Array not being appended to 在 swift 4.0 中将值附加到数组后如何重新加载 tableView - How to reload a tableView once a value has been appended to an array in swift 4.0 如何快速删除附加的搜索元素 - How to delete appended search elements in swift 使用调度等待数据附加到数组? - Using dispatch to wait for data to be appended to array? 如何使我在UIAlertController内的UITextField中输入的数据通过Swift中的重置保持不变? - How to make data I enter in a UITextField inside a UIAlertController persist through resets in Swift? 如何使用Swift使用Core Data更新/保存和保留非标准(可转换)属性? - How can I get non-standard (transformable) attributes to update/save and persist using Core Data with Swift? 如何在 Swift 4 中保留 Realm List 属性? - How to persist a Realm List property in Swift 4? 如何在核心数据中保留MSMutableArray? - How to persist an MSMutableArray in Core Data? 如何将另一个数组中的数组存储到Swift中从JSON传递的全局变量中? - How to store an array that is within an another array to a global variable that has been passed from JSON in Swift?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM