[英]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.