宽窄优行-由【嘉易行】项目成品而来
younger_times
2023-07-05 0d8f5fc8a516bfd07e425909e4a4432600572ee7
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
//
//  SearchHistorySave.swift
//  XiaoKeProject
//
//  Created by alvin_y on 2019/8/16.
//  Copyright © 2019 yangwang. All rights reserved.
//
 
import UIKit
 
// 文件地址
let accountPath = NSSearchPathForDirectoriesInDomains(.documentDirectory, .userDomainMask, true).first! + "/address.plist"
 
 
 
 
class SearchHistoryModel:  NSObject,Codable {
    
    var name = ""
}
 
class SearchHistorySave: NSObject {
    
    class func saveFile(model: SearchHistoryModel)  {
        
        var array = readFile()
        //去重
        for (value,item) in array.enumerated() {
            if item.name == model.name{
                array.remove(at: value)
            }
        }
        array.insert(model, at: 0)
        if array.count > 6{
            array.remove(at: 6)
        }
            NSKeyedArchiver.archiveRootObject(array, toFile: accountPath)
        
    }
    
    class func readFile() -> [SearchHistoryModel] {
        let array:[SearchHistoryModel] = NSKeyedUnarchiver.unarchiveObject(withFile: accountPath) as? [SearchHistoryModel] ?? []
        return array
    }
    
    class func clearFile() {
            NSKeyedArchiver.archiveRootObject([], toFile: accountPath)
    }
}