Swift-如何存储数据在本地
来源:互联网 发布:淘宝决斗盘 编辑:程序博客网 时间:2024/06/07 03:20
使用playground来测试存储数据和读取数据, 使用的是访问本地目录。
//: Playground - noun: a place where people can play
import UIKit
import Foundation
//var str = "Hello, playground"
class Note: NSObject, NSCoding {
let title : String
let text : String
let timestamp : Date
init(title: String, text:String, timestamp: Date) {
self.title = title
self.text = text
self.timestamp = timestamp
}
// public func encode(with aCoder: NSCoder) {
// aCoder.encode(title, forKey: "title")
// aCoder.encode(text, forKey: "text")
// aCoder.encode(timestamp, forKey: "timestamp")
//
// }
struct PropertyKey {
static let title ="title"
static let text ="text"
static let timestamp ="timestamp"
}
override var description:String {
return"Note(title: \"\(title)\", text: \"\(text)\",timestamp:\(timestamp))"
}
requiredconvenienceinit?(coder aDecoder:NSCoder) // NS_DESIGNATED_INITIALIZER
{
guard let title = aDecoder.decodeObject(forKey:PropertyKey.title)as? String,
let text = aDecoder.decodeObject(forKey:PropertyKey.text)as? String,
let timestamp = aDecoder.decodeObject(forKey:PropertyKey.timestamp)as? Date else { return nil }
self.init(title: title, text: text, timestamp: timestamp)
}
func encode(with aCoder: NSCoder) {
aCoder.encode(title, forKey:PropertyKey.title)
aCoder.encode(text, forKey:PropertyKey.text)
aCoder.encode(timestamp, forKey:PropertyKey.timestamp)
}
}
let note1 = Note(title:"A New Hope", text: "We have the plans.", timestamp: Date())
let note2 = Note(title:"A Dream", text: "We have a Dreem.", timestamp:Date())
let note3 = Note(title:"A Meeting", text: "We have two meetings.", timestamp: Date())
let notes = [note1,note2,note3]
//print(note)
//let archivedNote = NSKeyedArchiver.archivedData(withRootObject: note)
print(archivedNote)
//let unarchivedNote = NSKeyedUnarchiver.unarchiveObject(with: archivedNote) as? Note
let documentDirectory =FileManager.default.urls(for: .documentDirectory, in: .userDomainMask).first!
print(documentDirectory)
let archiveURL = documentDirectory.appendingPathComponent("notes")
print(archiveURL)
let archivedNote =NSKeyedArchiver.archiveRootObject(notes, toFile:archiveURL.path)
let unarchivedNote = NSKeyedUnarchiver.unarchiveObject(withFile:archiveURL.path) as? [Note]
//print(unarchivedNote!.title)
//print(unarchivedNote!.text)
//print(unarchivedNote!.timestamp)
print(unarchivedNote![2].title)
for i in 0...2 {
print("Title:\(unarchivedNote![i].title) and Text:\(unarchivedNote![i].text) and Text:\(unarchivedNote![i].timestamp)")
}
- Swift-如何存储数据在本地
- swift本地存储
- swift中NSUserDefaults的用法(轻量级本地数据存储)
- Swift - 使用NSUserDefaults来进行本地数据存储
- Swift - 使用NSUserDefaults来进行本地数据存储
- 在本地创建SQLite数据库存储数据
- 本地存储webstorage-如何监测本地数据是否改变
- PlayerPrefs存储数据在本地的存储位置
- PlayerPrefs存储数据在本地的存储位置
- 如何在AIR中加密本地数据
- 【IOS-Swift】实战笔记——数据持久化之NSUserDefaults(本地轻量级数据存储)
- Swift数据永久存储
- swift 数据存储
- Swift-网络、本地数据解析
- Swift NSUserDefaults本地数据保存
- python中如何把数据存储到本地
- android 本地存储数据
- 本地数据存储----SharedPreferences
- 论一个python程序猿的个人修养(一)
- 基础语法C语言——第一个程序HelloWorld
- 多线程基础之六:Pthread Win32实现的非阻塞请求机制的Semaphore
- 易错3(pow)
- java获得项目绝对路径
- Swift-如何存储数据在本地
- vba基础打卡4
- maven 学习(二)
- ubuntu关机重启命令介绍
- C语言的二维数组、及多维数组的降维
- java8的日期API总结(JSR310)
- 转载-【实用】如何将sublime text 3 打造成实用的python IDE 环境
- PHP & AJAX 阅读器
- JS中的while循环