[英]Persist data between app launches
I have a class to handle a simple note creator in my app. 我有一个类可以处理我的应用程序中的简单笔记创建者。 At the moment, notes are stored using an array of custom Note objects. 目前,便笺使用一组自定义便笺对象存储。 How can I save the contents of this array when the app closes and load them again when the app is re-opened? 当应用程序关闭时,如何保存该数组的内容;当应用程序重新打开时,如何再次加载它们? I've tried NSUserDefaults, but I can't figure out how to save the array since it isn't just comprised of Strings. 我已经尝试过NSUserDefaults,但是我不知道如何保存数组,因为它不仅由字符串组成。
Code: 码:
Note.swift Note.swift
class Note {
var contents: String
// an automatically generated note title, based on the first line of the note
var title: String {
// split into lines
let lines = contents.componentsSeparatedByCharactersInSet(NSCharacterSet.newlineCharacterSet()) as [String]
// return the first
return lines[0]
}
init(text: String) {
contents = text
}
}
var notes = [
Note(text: "Contents of note"),]
There are different approaches to this. 有不同的方法。
The easiest would be to adopt NSCoding
, let Note
inherit from NSObject
and use NSKeyedArchiver
and NSKeyedUnarchiver
to write to/from files in the app's sandbox. 最简单的方法是采用NSCoding
,让Note
从NSObject
继承,并使用NSKeyedArchiver
和NSKeyedUnarchiver
来写入应用程序沙箱中的文件或从中写入文件。
Here is a trivial example for this: 这是一个简单的例子:
final class Feedback : NSObject, NSCoding {
private static let documentsPath = NSSearchPathForDirectoriesInDomains(.DocumentDirectory, .UserDomainMask, true)[0]
let content : String
let entry : EntryId
let positive : Bool
let date : NSDate
init(content: String, entry: EntryId, positive : Bool, date :NSDate = NSDate()) {
self.content = content
self.entry = entry
self.positive = positive
self.date = date
super.init()
}
@objc init?(coder: NSCoder) {
if let c = coder.decodeObjectForKey("content") as? String,
let d = coder.decodeObjectForKey("date") as? NSDate {
let e = coder.decodeInt32ForKey("entry")
let p = coder.decodeBoolForKey("positive")
self.content = c
self.entry = e
self.positive = p
self.date = d
}
else {
content = ""
entry = -1
positive = false
date = NSDate()
}
super.init()
if self.entry == -1 {
return nil
}
}
@objc func encodeWithCoder(aCoder: NSCoder) {
aCoder.encodeBool(self.positive, forKey: "positive")
aCoder.encodeInt32(self.entry, forKey: "entry")
aCoder.encodeObject(content, forKey: "content")
aCoder.encodeObject(date, forKey: "date")
}
static func feedbackForEntry(entry: EntryId) -> Feedback? {
let path = Feedback.documentsPath.stringByAppendingString("/\(entry).feedbackData")
if let success = NSKeyedUnarchiver.unarchiveObjectWithFile(path) as? Feedback {
return success
}
else {
return nil
}
}
func save() {
let path = Feedback.documentsPath.stringByAppendingString("/\(entry).feedbackData")
let s = NSKeyedArchiver.archiveRootObject(self, toFile: path)
if !s {
debugPrint("Warning: did not save a Feedback for \(self.entry): \"\(self.content)\"")
}
}
}
The more efficient but more complex solution is using Core Data, Apple's ORM-Framework - which's usage is way beyond the scope of a SO answer. 更为有效但更复杂的解决方案是使用Apple的ORM框架Core Data-这种用法超出了SO答案的范围。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.