File tree

38 files changed

+2081
-2
lines changed

38 files changed

+2081
-2
lines changed
Large diffs are not rendered by default.
Some generated files are not rendered by default. Learn more about customizing how changed files appear on .
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,46 @@
1+
//
2+
// AppDelegate.swift
3+
// CacheDemo_Archive
4+
//
5+
// Created by 韩俊强 on 2017/8/25.
6+
// Copyright © 2017年 HaRi. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
@UIApplicationMain
12+
class AppDelegate: UIResponder, UIApplicationDelegate {
13+
14+
var window: UIWindow?
15+
16+
17+
func application(_ application: UIApplication, didFinishLaunchingWithOptions launchOptions: [UIApplicationLaunchOptionsKey: Any]?) -> Bool {
18+
// Override point for customization after application launch.
19+
return true
20+
}
21+
22+
func applicationWillResignActive(_ application: UIApplication) {
23+
// Sent when the application is about to move from active to inactive state. This can occur for certain types of temporary interruptions (such as an incoming phone call or SMS message) or when the user quits the application and it begins the transition to the background state.
24+
// Use this method to pause ongoing tasks, disable timers, and invalidate graphics rendering callbacks. Games should use this method to pause the game.
25+
}
26+
27+
func applicationDidEnterBackground(_ application: UIApplication) {
28+
// Use this method to release shared resources, save user data, invalidate timers, and store enough application state information to restore your application to its current state in case it is terminated later.
29+
// If your application supports background execution, this method is called instead of applicationWillTerminate: when the user quits.
30+
}
31+
32+
func applicationWillEnterForeground(_ application: UIApplication) {
33+
// Called as part of the transition from the background to the active state; here you can undo many of the changes made on entering the background.
34+
}
35+
36+
func applicationDidBecomeActive(_ application: UIApplication) {
37+
// Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
38+
}
39+
40+
func applicationWillTerminate(_ application: UIApplication) {
41+
// Called when the application is about to terminate. Save data if appropriate. See also applicationDidEnterBackground:.
42+
}
43+
44+
45+
}
46+
File renamed without changes.
File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,26 @@
1+
<?xml version="1.0" encoding="UTF-8" standalone="no"?>
2+
<document type="com.apple.InterfaceBuilder3.CocoaTouch.Storyboard.XIB" version="3.0" toolsVersion="11134" systemVersion="15F34" targetRuntime="iOS.CocoaTouch" propertyAccessControl="none" useAutolayout="YES" useTraitCollections="YES" colorMatched="YES" initialViewController="BYZ-38-t0r">
3+
<dependencies>
4+
<plugIn identifier="com.apple.InterfaceBuilder.IBCocoaTouchPlugin" version="11106"/>
5+
<capability name="documents saved in the Xcode 8 format" minToolsVersion="8.0"/>
6+
</dependencies>
7+
<scenes>
8+
<!--View Controller-->
9+
<scene sceneID="tne-QT-ifu">
10+
<objects>
11+
<viewController id="BYZ-38-t0r" customClass="ViewController" customModuleProvider="target" sceneMemberID="viewController">
12+
<layoutGuides>
13+
<viewControllerLayoutGuide type="top" id="y3c-jy-aDJ"/>
14+
<viewControllerLayoutGuide type="bottom" id="wfy-db-euE"/>
15+
</layoutGuides>
16+
<view key="view" contentMode="scaleToFill" id="8bC-Xf-vdC">
17+
<rect key="frame" x="0.0" y="0.0" width="375" height="667"/>
18+
<autoresizingMask key="autoresizingMask" widthSizable="YES" heightSizable="YES"/>
19+
<color key="backgroundColor" red="1" green="1" blue="1" alpha="1" colorSpace="custom" customColorSpace="sRGB"/>
20+
</view>
21+
</viewController>
22+
<placeholder placeholderIdentifier="IBFirstResponder" id="dkx-z0-nzr" sceneMemberID="firstResponder"/>
23+
</objects>
24+
</scene>
25+
</scenes>
26+
</document>
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,112 @@
1+
//
2+
// Content.swift
3+
// CacheDemo_Archive
4+
//
5+
// Created by 韩俊强 on 2017/8/25.
6+
// Copyright © 2017年 HaRi. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
// 1.1 需要遵循NSCoding协议
12+
13+
// 1.2 需要实现func encode(with aCoder: NSCoder){}归档方法
14+
15+
// 1.3 需要实现 required init(coder aDecoder: NSCoder){}解档方法
16+
17+
// 1.4 重写init方法
18+
19+
class Content: NSObject, NSCoding {
20+
21+
var id:Int?
22+
23+
var nickname:String?
24+
25+
var phone:String?
26+
27+
var account:String?
28+
29+
var password:String?
30+
31+
var type:Int?
32+
33+
var icon:String?
34+
35+
var attentionnumber:Int?
36+
37+
var registertime:String?
38+
39+
var qrcode:String?
40+
41+
var signature:String?
42+
43+
var dynamicstruts:Int?
44+
45+
var score:Int?
46+
47+
func encode(with aCoder: NSCoder) {
48+
49+
aCoder.encode(id, forKey:"id")
50+
51+
aCoder.encode(nickname, forKey:"nickname")
52+
53+
aCoder.encode(phone, forKey:"phone")
54+
55+
aCoder.encode(account, forKey:"account")
56+
57+
aCoder.encode(password, forKey:"password")
58+
59+
aCoder.encode(type, forKey:"type")
60+
61+
aCoder.encode(icon, forKey:"icon")
62+
63+
aCoder.encode(attentionnumber, forKey:"attentionnumber")
64+
65+
aCoder.encode(registertime, forKey:"registertime")
66+
67+
aCoder.encode(qrcode, forKey:"qrcode")
68+
69+
aCoder.encode(signature, forKey:"signature")
70+
71+
aCoder.encode(dynamicstruts, forKey:"dynamicstruts")
72+
73+
aCoder.encode(score, forKey:"score")
74+
}
75+
76+
required init?(coder aDecoder: NSCoder) {
77+
super.init()
78+
79+
id = aDecoder.decodeObject(forKey:"id")as?Int
80+
81+
nickname = aDecoder.decodeObject(forKey:"nickname")as?String
82+
83+
phone = aDecoder.decodeObject(forKey:"phone")as?String
84+
85+
account = aDecoder.decodeObject(forKey:"account")as?String
86+
87+
password = aDecoder.decodeObject(forKey:"password")as?String
88+
89+
type = aDecoder.decodeObject(forKey:"type")as?Int
90+
91+
icon = aDecoder.decodeObject(forKey:"icon")as?String
92+
93+
attentionnumber = aDecoder.decodeObject(forKey:"attentionnumber")as? Int
94+
95+
registertime = aDecoder.decodeObject(forKey:"registertime")as?String
96+
97+
qrcode = aDecoder.decodeObject(forKey:"qrcode")as?String
98+
99+
signature = aDecoder.decodeObject(forKey:"signature")as?String
100+
101+
dynamicstruts = aDecoder.decodeObject(forKey:"dynamicstruts")as? Int
102+
103+
score = aDecoder.decodeObject(forKey:"score")as?Int
104+
}
105+
106+
override init() {
107+
108+
super.init()
109+
110+
}
111+
}
112+
File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,62 @@
1+
//
2+
// ViewController.swift
3+
// CacheDemo_Archive
4+
//
5+
// Created by 韩俊强 on 2017/8/25.
6+
// Copyright © 2017年 HaRi. All rights reserved.
7+
//
8+
9+
import UIKit
10+
11+
class ViewController: UIViewController {
12+
13+
// 3.0 实现归档把模型保存到本地Document文件夹:
14+
// 3.1 获取本地Document路径,一般路径都设为全局变量,方便解档直接使用:
15+
let userAccountPath = "\(NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory,FileManager.SearchPathDomainMask.userDomainMask,true).first!)/userAccount.data"
16+
17+
override func viewDidLoad() {
18+
super.viewDidLoad()
19+
20+
let userModel = Content()
21+
userModel.id = 419
22+
userModel.nickname = "小韩哥"
23+
userModel.phone = "1588888888"
24+
25+
// 归档
26+
saveWriteNSKeyedArchiver(userModel: userModel)
27+
28+
// 解档
29+
readWithNSKeyedUnarchiver { (model) in
30+
print(model.nickname!)
31+
}
32+
}
33+
34+
// MARK - 归档
35+
func saveWriteNSKeyedArchiver(userModel : Content) {
36+
37+
// 3.2 对获取到的模型进行归档操作,要注意模型必须是确定的类型,如果是可选类型会报发送未识别的消息的错误(切记)
38+
NSKeyedArchiver.archiveRootObject(userModel, toFile:userAccountPath)
39+
}
40+
41+
// MARK: - 解档
42+
// 4.实现解档从Document文件夹获取本地模型数据
43+
// 4.1 判断Document文件夹下是否有已保存好的模型,有的话就解档取出模型
44+
func readWithNSKeyedUnarchiver(finished: @escaping (Content) ->()) -> () {
45+
DisQueue.global().async {
46+
() -> Void in
47+
if NSKeyedUnarchiver.unarchiveObject(withFile:self.userAccountPath) != nil {
48+
49+
let userModel = (NSKeyedUnarchiver.unarchiveObject(withFile:self.userAccountPath) as? Content)!
50+
finished(userModel)
51+
}
52+
}
53+
}
54+
55+
override func didReceiveMemoryWarning() {
56+
super.didReceiveMemoryWarning()
57+
// Dispose of any resources that can be recreated.
58+
}
59+
60+
61+
}
62+
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// CacheDemo_ArchiveTests.swift
3+
// CacheDemo_ArchiveTests
4+
//
5+
// Created by 韩俊强 on 2017/8/25.
6+
// Copyright © 2017年 HaRi. All rights reserved.
7+
//
8+
9+
import XCTest
10+
@testable import CacheDemo_Archive
11+
12+
class CacheDemo_ArchiveTests: XCTestCase {
13+
14+
override func setUp() {
15+
super.setUp()
16+
// Put setup code here. This method is called before the invocation of each test method in the class.
17+
}
18+
19+
override func tearDown() {
20+
// Put teardown code here. This method is called after the invocation of each test method in the class.
21+
super.tearDown()
22+
}
23+
24+
func testExample() {
25+
// This is an example of a functional test case.
26+
// Use XCTAssert and related functions to verify your tests produce the correct results.
27+
}
28+
29+
func testPerformanceExample() {
30+
// This is an example of a performance test case.
31+
self.measure {
32+
// Put the code you want to measure the time of here.
33+
}
34+
}
35+
36+
}
File renamed without changes.
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,36 @@
1+
//
2+
// CacheDemo_ArchiveUITests.swift
3+
// CacheDemo_ArchiveUITests
4+
//
5+
// Created by 韩俊强 on 2017/8/25.
6+
// Copyright © 2017年 HaRi. All rights reserved.
7+
//
8+
9+
import XCTest
10+
11+
class CacheDemo_ArchiveUITests: XCTestCase {
12+
13+
override func setUp() {
14+
super.setUp()
15+
16+
// Put setup code here. This method is called before the invocation of each test method in the class.
17+
18+
// In UI tests it is usually best to stop immediately when a failure occurs.
19+
continueAfterFailure = false
20+
// UI tests must launch the application that they test. Doing this in setup will make sure it happens for each test method.
21+
XCUIApplication().launch()
22+
23+
// In UI tests it’s important to set the initial state - such as interface orientation - required for your tests before they run. The setUp method is a good place to do this.
24+
}
25+
26+
override func tearDown() {
27+
// Put teardown code here. This method is called after the invocation of each test method in the class.
28+
super.tearDown()
29+
}
30+
31+
func testExample() {
32+
// Use recording to get started writing UI tests.
33+
// Use XCTAssert and related functions to verify your tests produce the correct results.
34+
}
35+
36+
}
File renamed without changes.

0 commit comments

Comments
 (0)