Skip to content
/ Coding Public

Coding is more Swifty NSCoding. It can archive even pure Swift class and struct.

License

Notifications You must be signed in to change notification settings

tattn/Coding

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

4 Commits
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 
 

Repository files navigation

Coding

Carthage compatible Platform License Swift Version

Coding is more Swifty NSCoding. It can archive even pure Swift class and struct.

How to use

struct DataModel: Coding {
    let string: String
    let integer: Int
    let double: Double
    
    enum Key: String {
        case string
        case integer
        case double
    }
    
    func encode(with coder: Encoder<Key>) {
        coder.encode(string, for: .string)
        coder.encode(integer, for: .integer)
        coder.encode(double, for: .double)
    }
    
    static func decode(with decoder: Decoder<Key>) -> DataModel? {
        guard let string = decoder.decodeString(for: .string) else { return nil }
        let integer = decoder.decodeInteger(for: .integer)
        let double = decoder.decodeDouble(for: .double)
        return DataModel(string: string, integer: integer, double: double)
    }
}

let model: DataModel = DataModel(string: "Hello", integer: 123, double: 3.14)
let data: Data = model.archive()
let unarchivedModel: DataModel = DataModel.unarchive(with: data)

Installation

Carthage

github "tattn/Coding"

Contributing

  1. Fork it!
  2. Create your feature branch: git checkout -b my-new-feature
  3. Commit your changes: git commit -am 'Add some feature'
  4. Push to the branch: git push origin my-new-feature
  5. Submit a pull request :D

License

Coding is released under the MIT license. See LICENSE for details.

About

Coding is more Swifty NSCoding. It can archive even pure Swift class and struct.

Topics

Resources

License

Stars

Watchers

Forks

Packages

No packages published