Skip to content

Instantly share code, notes, and snippets.

@feighter09
Created October 24, 2016 06:17
Show Gist options
  • Save feighter09/23b9172f5b0072e81f021e3413be385a to your computer and use it in GitHub Desktop.
Save feighter09/23b9172f5b0072e81f021e3413be385a to your computer and use it in GitHub Desktop.

Revisions

  1. feighter09 revised this gist Oct 24, 2016. 1 changed file with 0 additions and 1 deletion.
    1 change: 0 additions & 1 deletion KeylessUserDefaultsPersonExtension.swift
    Original file line number Diff line number Diff line change
    @@ -1,4 +1,3 @@
    // Conform to DefaultConvertible
    extension Person: DefaultConvertible {
    private static let firstNameKey = "first_name"
    private static let lastNameKey = "last_name"
  2. feighter09 created this gist Oct 24, 2016.
    27 changes: 27 additions & 0 deletions KeylessUserDefaultsPersonExtension.swift
    Original file line number Diff line number Diff line change
    @@ -0,0 +1,27 @@
    // Conform to DefaultConvertible
    extension Person: DefaultConvertible {
    private static let firstNameKey = "first_name"
    private static let lastNameKey = "last_name"
    private static let ageKey = "age"

    var serialized: [String : AnyObject] {
    return
    [
    Person.firstNameKey: firstName as AnyObject,
    Person.lastNameKey: lastName as AnyObject,
    Person.ageKey: age as AnyObject
    ]
    }

    init?(dictionary: [String : AnyObject])
    {
    guard let firstName = dictionary[Person.firstNameKey] as? String,
    let lastName = dictionary[Person.lastNameKey] as? String,
    let age = dictionary[Person.ageKey] as? Int
    else { return nil }

    self.firstName = firstName
    self.lastName = lastName
    self.age = age
    }
    }