Class & Struct 的 NSCoding 一

作者: NinthDay | 来源:发表于2017-03-12 11:33 被阅读607次

    1.Class 类型的 NSCoding

    1.1 正儿八经的使用方式

    前提一定是要继承NSObject,实现NSCoding协议,和 OC 基本差不多。

    class Coordinate:NSObject,NSCoding {
        let latitude:Double = 10
        let longitude:Double = 10
        
        init(latitude:Double,longitude:Double) {
            self.latitude = latitude
            self.longitude = longitude
        }
    
        convenience required init?(coder aDecoder: NSCoder) {
          self.init()
          self.latitude = aDecoder.decodeObject(forKey: "latitude")
          self.longitude = aDecoder.decodeObject(forKey: "longitude")
        }
        
        func encode(with aCoder: NSCoder) {
            aCoder.encode(coordinate?.latitude,forKey:"latitude")
            aCoder.encode(coordinate?.longitude,forKey:"longitude")
        }
    }
    

    1.2 Mirror基础知识

    下面是一个简单的例子:

    class Project {
        var title: String = ""
        var id: Int = 0
        var platform: String = ""
        var version: Int = 0
        var info: String?
    }
    

    创建一个实例,得到其反射结果。我们可以得到的东西很多,类型,值,标签等等

    let sampleProject = Project()
    sampleProject.title = "MirrorMirror"
    sampleProject.id = 199
    sampleProject.platform = "iOS"
    sampleProject.version = 2
    sampleProject.info = "test app for Reflection"
    The code below shows the creating of Mirror instance. The children property of the mirror is a AnyForwardCollection<Child> where Child is typealias tuple for subject's property and value. Child had a label: String and value: Any.
    
    let projectMirror = Mirror(reflecting: sampleProject)
    let properties = projectMirror.children
    
    print(properties.count)        //5
    print(properties.first?.label) //Optional("title")
    print(properties.first!.value) //MirrorMirror
    print()
    
    for property in properties {
        print("\(property.label!):\(property.value)")
    }
    

    终端输出如下:

    title:MirrorMirror
    id:199
    platform:iOS
    version:2
    info:Optional("test app for Reflection")
    

    Tested in Playground on Xcode 8 beta 2

    语法:

    Mirror(reflecting: instance) // Initializes a mirror with the subject to reflect
    mirror.displayStyle // 这里就是 Class 当让也会是struct
    mirror.description // 其实就是 CustomStringConvertible 协议的描述
    mirror.subjectType // 返回被反射的对象的类型 这里是Project
    mirror.superclassMirror // 返回被反射的对象的父类的类型 这里没有就是nil
    

    如果你想完整的看Mirror用法,请前往swiftgg 阅读 Swift 反射 API 及用法 一文。

    1.3 使用Mirror改写NSCoding

    /// 继承 NSCoding 协议的 Class
    class PersonClass:NSObject,NSCoding{
        var name:String = "pmst"
        var age:Int = 20
        
        override var description: String{
            return "name:\(name) age:\(age)"
        }
        
        override init() {
            super.init()
        }
        
        convenience required init?(coder aDecoder: NSCoder) {
            self.init()
            
            var mirror:Mirror? = Mirror(reflecting: self)
            repeat {
                // typealias Children = AnyCollection<Mirror.Child>
                // typealias Child = (label: String?, value: Any)
                // 以上是额外的知识点
                for case let (label?,value) in mirror!.children {
                    setValue(value, forKey: label)
                }
                mirror = mirror!.superclassMirror
                
            } while mirror != nil
        }
        
        func encode(with aCoder: NSCoder) {
            var mirror:Mirror? = Mirror(reflecting: self)
            repeat {
                
                // typealias Children = AnyCollection<Mirror.Child>
                // typealias Child = (label: String?, value: Any)
                // 以上是额外的知识点
                for case let (label?,value) in mirror!.children {
                    aCoder.encode(value, forKey: label)
                }
                mirror = mirror!.superclassMirror
                
            } while mirror != nil
        }
    }
    
    
    class Teacher:PersonClass{
        var course = "英文"
        var workAge = 10
        
        override var description: String{
            return super.description + " course:\(course)" + "workAge:\(workAge)"
        }
    }
    

    实战:

    let encodePerson = PersonClass()
    let documentsPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).first
    let path = documentsPath?.appending("/person1")
    print(path)
    let data = NSKeyedArchiver.archiveRootObject(encodePerson, toFile: path!)
    print(encodePerson)
    encodePerson.name = "machao"
    encodePerson.age = 18
    print(encodePerson)
    
    // 解压出来
    let decodePerson = NSKeyedUnarchiver.unarchiveObject(withFile: path!)
    print(decodePerson)
    
    let encodeTeacher = Teacher()
    let path1 = documentsPath?.appending("/teacher")
    let data1 = NSKeyedArchiver.archiveRootObject(encodeTeacher, toFile: path1!)
    let decodeTeacher = NSKeyedUnarchiver.unarchiveObject(withFile: path1!)
    print(decodeTeacher)
    

    2. Struct 类型的 NSCoding

    NSCoding 协议定义了 encodedecode,另外 Swift 必须遵循 NSObjectProtocol ,顾名思义只适用类对象,结构体 struct 显然被排除在外。那么如何让结构体也能达到 encodedecode呢?

    创建一个专门负责encodedecode,这种做法在设计模式中应该叫做策略模式?单一职责原则也是我们所提倡的。下面介绍几种实现方式,本质都是需要创建一个类。下面介绍几种方式。

    2.1 方式一

    代码如下:

    /// 坐标的基本数据结构
    struct Coordinate {
        let latitude:Double
        let longitude:Double
        
        init(latitude:Double,longitude:Double) {
            self.latitude = latitude
            self.longitude = longitude
        }
    }
    
    /// 搞一个类负责encoding 这样可以更严格地适用单一职责原则
    class EncodingCoordinate:NSObject,NSCoding{
        var coordinate:Coordinate?
        init(coordinate:Coordinate?) {
            self.coordinate = coordinate
        }
        
        required init?(coder aDecoder: NSCoder) {
            guard
                let latitude = aDecoder.decodeObject(forKey: "latitude") as? Double,
                let longitude = aDecoder.decodeObject(forKey: "longitude") as? Double else {
                return nil
            }
            coordinate = Coordinate(latitude: latitude, longitude: longitude)
        }
        
        func encode(with aCoder: NSCoder) {
            aCoder.encode(coordinate?.latitude,forKey:"latitude")
            aCoder.encode(coordinate?.longitude,forKey:"longitude")
        }
    }
    let coordinate = Coordinate(latitude: 12.0, longitude: 13.1)
    let encodable = EncodingCoordinate(coordinate: coordinate)
    let documentsPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).first
    let path = documentsPath?.appending("/Coordinate")
    print(path)
    let data = NSKeyedArchiver.archiveRootObject(encodable, toFile: path!)// 关键!archiveRootObject的对象一定是实现了NSCoding协议的
    

    优点:把encode和decode的职责单独搞成了一个类,我们使用时只需要传入结构体实例coordinate得到一个EncodingCoordinate类型的实例encodable,然后拿它来归档和接档操作。
    缺点:每次都要为我们结构体单独创建一个归档解档的类,而且都是分离的,内聚性低。

    因此我们尝试将归档解档的类嵌入到结构体中。

    2.2 方式二

    我们最终会调用结构体的encode和decode,但是我们会依靠HelperClass类,正如你看到的真正归档和解档的操作是由HelperClass来实现的,它实现NSCoding协议,和方式一的做法一样,我们会传入person对象。

    struct Person{
        let firstName:String
        let lastName:String
    
        static func encode(person: Person) {
            let personClassObject = HelperClass(person: person)
    
            NSKeyedArchiver.archiveRootObject(personClassObject, toFile: HelperClass.path())
        }
    
        static func decode() -> Person? {
            let personClassObject = NSKeyedUnarchiver.unarchiveObject(withFile: HelperClass.path()) as? HelperClass
    
            return personClassObject?.person
        }
    }
    
    extension Person{
        class HelperClass:NSObject,NSCoding {
            var person:Person?
    
            init(person:Person){
                super.init()
                self.person = person
            }
            class func path() -> String {
                let documentsPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).first
                let path = documentsPath?.appending("/Person")
                return path!
            }
    
            required init?(coder aDecoder: NSCoder) {
                super.init()
                
                guard let firstName = aDecoder.decodeObject(forKey: "firstName") as? String else { person = nil; return nil }
                guard let laseName = aDecoder.decodeObject(forKey: "lastName") as? String else { person = nil;  return nil }
    
                person = Person(firstName: firstName, lastName: laseName)
            }
    
            func encode(with aCoder: NSCoder) {
                aCoder.encode(person!.firstName, forKey: "firstName")
                aCoder.encode(person!.lastName, forKey: "lastName")
            }
        }
    }
    

    这里似乎我们只是进步了一点点,内聚性相对高一些,但是又遵循了单一职责原则。

    2.3 方式三

    swiftgg 结构体与 NSCoding 一文提供协议方式,但是我感觉还是局限性很大,只是为了结合Cache来做一些东西。接着Coordinate的代码改进,下面贴出代码:

    /// 定义两个协议 前者是
    protocol Encoded {
        associatedtype Encoder: NSCoding
        
        var encoder: Encoder { get } // 要求像Coordinate结构体对象提供一个实例,负责归档解档职责。ps:有点绕。
    }
    
    protocol Encodable {
        associatedtype Value
        
        var value: Value? { get }
    }
    
    extension EncodableCoordinate: Encodable {
        var value: Coordinate? {
            return coordinate
        }
    }
    
    extension Coordinate: Encoded {
        var encoder: EncodableCoordinate {
            return EncodableCoordinate(coordinate: self)
        }
    }
    
    /// cache对象 save和fetch两个操作分别对象归档和解档行为 这里的泛型约束很重要
    /// T 首先是要遵循Encoded协议,提供一个归档解档类;接着T的关联类型 Encoder 要遵循Encodable协议,为的就是让encoder返回解档的值;最后保证解档的值的类型和T一致
    class Cache<T: Encoded> where T.Encoder: Encodable, T.Encoder.Value == T {
        //...
        func save(object: T) {
        NSKeyedArchiver.archiveRootObject(object.encoder, toFile: path)
      }
      
      func fetchObject() -> T? {
        let fetchedEncoder = NSKeyedUnarchiver.unarchiveObject(withFile: storagePath)
        let typedEncoder = fetchedEncoder as? T.Encoder
        return typedEncoder?.value as T?
      }
    }
    
    /// 使用
    let cache = Cache<Coordinate>(name: "coordinateCache")
    cache.save(object: coordinate)
    

    2.4 另类的一种方式

    实现思路是先转成字典,然后将字典归档解档。这里只是提供一种思路,但是只是个雏形,尽管结构体只要实现一个方法即可,但是感觉还是不太优雅。

    代码如下:

    
    protocol StructDecoder {
        static func dictionaryTo(dict:Dictionary<String,Any>)->Self
        static func path()->String
        static func decode()->Self
        func encode()
    }
    
    extension StructDecoder {
        var mirrorObject:Mirror {
            return Mirror(reflecting: self)
        }
        
        static func path()->String {
            let documentsPath = NSSearchPathForDirectoriesInDomains(FileManager.SearchPathDirectory.documentDirectory, FileManager.SearchPathDomainMask.userDomainMask, true).first
            let path = documentsPath?.appending("/\(Self.self)")
            return path!
        }
        
        func toDictionary() -> Dictionary<String, Any> {
            var mirror:Mirror? = mirrorObject
            var dict:[String:Any] = [:]
            repeat {
                
                for case let (label?,value) in mirror!.children {
                    dict[label] = value
                }
                mirror = mirror!.superclassMirror
                
            } while mirror != nil
            
            return dict
        }
        
        func encode(){
            NSKeyedArchiver.archiveRootObject(toDictionary(), toFile:Self.path())
        }
    
        static func decode()->Self {
            let dict = NSKeyedUnarchiver.unarchiveObject(withFile: path()) as! [String:Any]
            
            return Self.dictionaryTo(dict: dict)
        }
        
    }
    
    struct STPerson:StructDecoder {
    
        var name:String = "pmst"
        var age:Int = 20
        
        static func dictionaryTo(dict: Dictionary<String, Any>) -> STPerson {
            return STPerson(name: dict["name"] as! String, age: dict["age"] as! Int)
        }
    }
    

    可以看到只要实现dictionaryTo方法即可,原本我是希望也能默认实现的,但是发现貌似有点麻烦,暂且搁置。具体使用代码也很简单:

    var stPerson = STPerson()
    print(stPerson)
    stPerson.age = 100
    stPerson.name = "添加"
    stPerson.encode()
    var stPerson1 = STPerson.decode()
    print(stPerson1)
    

    优点:丑陋的一面被我隐藏在了extension里,你也做的不过是给你一个字典,自己映射到Model中。
    缺点:一个对象只能归档到一个文件,当然这个只需要少许改动代码即可;用反射的归档解档效率肯定低,目前看来是致命伤!

    如果有好的思路想法,请在下面给我留言哈。

    新浪微博:Ninth_Day

    相关文章

      网友评论

      • SR2k:感谢🙏!
        by the way 简书的 Markdown 的代码块在指定语言之后是可以高亮的:
        ```swift
        // 这里是代码
        ```
        NinthDay:原来已经支持了,之前用没效果就没加,感谢你的意见:blush:
      • xiAo__Ju:完美

      本文标题:Class & Struct 的 NSCoding 一

      本文链接:https://www.haomeiwen.com/subject/doytnttx.html