Swift 知识点总结

1.单粒

// 创建单粒类
class SingletonClass2 {
    static let shared = SingletonClass2()
    private init(){}
}

// 打印对象的内存地址
let address = Unmanaged.passUnretained(SingletonClass2.shared).toOpaque()
let address1 = Unmanaged.passUnretained(SingletonClass2.shared).toOpaque()
let address2 = Unmanaged.passUnretained(SingletonClass2.shared).toOpaque()
let address3 = Unmanaged.passUnretained(SingletonClass2.shared).toOpaque()

print(address)
print(address1)
print(address2)
print(address3)
/*
0x00006000024595f0
0x00006000024595f0
0x00006000024595f0
0x00006000024595f0
*/

2.存储对象(归档/解归档)

  • 用到 KakaJSON 框架

工具类 ArchiveTool.swift

//  归档和解归档工具类

import UIKit
import KakaJSON

class ArchiveTool {
    /// 文件夹名称
    static let kArchive = "archive"
    
    
    /// 存储归档
    ///
    /// - Parameter object: 要归档的对象
    /// - Returns: YES: 归档成功  NO: 归档失败
    static func saveObject(object: Any) {
        // 存放的文件路径
        let path = getPathWithPrefix(classType: object)
       
        // 保存
        write(object, to: path)
        
    }
    
    /// 删除归档
    ///
    /// - Parameter classType: 要删除归档的类对象
    /// - Returns: true: 删除成功  false: 删除失败
    static func deleteArchiveClass(classType: Any) -> Bool {
        // 获取类名
        let className = String(describing: type(of: classType))
        if className.isEmpty { // 没有值
            return false
        }
        let tempCaches = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.cachesDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last
        guard let caches = tempCaches  else { return true }
        let filePathFolder = caches + "/" + kArchive
        if !FileManager.default.fileExists(atPath: filePathFolder) { // 不存在
            return true
        }
        let fileName = filePathFolder + "/" + className + ".archive"
        do {
            try FileManager.default.removeItem(atPath: fileName)
            return true
        } catch  {
            print("\(classType)--删除归档失败")
            return false
        }
        
    }
    
    /// 获取类名
    ///
    /// - Parameter classType: 类对象
    /// - Returns: 类名
    static func getClassName(classType: Any) -> String {
        let className = String(describing: type(of: classType))
        return className
    }
    
    
    /// 存放的文件路径
    ///
    /// - Parameter prefix: 文件名称
    /// - Returns: 文件的路径
    static func getPathWithPrefix(classType: Any) -> String {
        // 获取类名
        let className = String(describing: type(of: classType))
        if className.isEmpty { // 没有值
            return ""
        }
        let tempCaches = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.cachesDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).last
        guard let caches = tempCaches  else { return "" }
        let filePathFolder = caches + "/" + kArchive
        
        if !FileManager.default.fileExists(atPath: filePathFolder) { // 不存在
            do {
                try FileManager.default.createDirectory(atPath: filePathFolder, withIntermediateDirectories: true, attributes: nil)
            }catch {
                print("存放的文件路径 error", error)
                
                return ""
            }
        }
        return filePathFolder + "/" + className + ".archive"
    }
    
}

使用示例

// 创建结构体 继承Convertible
struct Person: Convertible {
    var name: String = "浅宇洛"
    var age: Int = 26
}

// 存储Person对象
ArchiveTool.saveObject(object: Person())

// 读取Person对象
let model = read(Person.self, from: ArchiveTool.getPathWithPrefix(classType: Person()))
print("测试模型", model ?? AllModel())
// 测试模型 Person(name: "浅宇洛", age: 26)

// 删除归档的Person对象
let result = ArchiveTool.deleteArchiveClass(classType: Person())
print(result)
// true

你可能感兴趣的:(Swift 知识点总结)