Created
November 13, 2017 06:01
-
-
Save n1tesh/4de0fea9514e392e35e241786f51e98b to your computer and use it in GitHub Desktop.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import ContactsUI | |
struct User { | |
var userContact = CNContact() | |
var recipientContact: CNContact? | |
var isDefault: Bool = false | |
init(userContact: CNContact, recipientContact: CNContact? = nil, isDefault: Bool) { | |
self.userContact = userContact | |
self.recipientContact = recipientContact | |
self.isDefault = isDefault | |
} | |
} | |
extension User { | |
@objc(_TtCV6testUD4User6Coding)class Coding: NSObject, NSCoding { | |
let model: User? | |
init(model: User) { | |
self.model = model | |
super.init() | |
} | |
func encode(with aCoder: NSCoder) { | |
guard let model = self.model else { | |
return | |
} | |
aCoder.encode(model.userContact, forKey: "userContact") | |
aCoder.encode(model.recipientContact, forKey: "recipientContact") | |
aCoder.encode(model.isDefault, forKey: "isDefault") | |
} | |
required init?(coder aDecoder: NSCoder) { | |
guard let userContact = aDecoder.decodeObject(forKey: "userContact") as? CNContact,let isDefault = aDecoder.decodeObject(forKey: "isDefault") as? Bool else { | |
return nil | |
} | |
let recipientContact = aDecoder.decodeObject(forKey: "recipientContact") as? CNContact | |
model = User(userContact: userContact, recipientContact: recipientContact, isDefault: isDefault) | |
super.init() | |
} | |
} | |
} | |
extension User: Encodable { | |
var encoded: Decodable? { | |
return User.Coding(model: self) | |
} | |
} | |
extension User.Coding: Decodable { | |
var decoded: Encodable? { | |
return self.model | |
} | |
} | |
extension Sequence where Iterator.Element: Encodable { | |
var encoded: [Decodable] { | |
return self.filter({ $0.encoded != nil }).map({ $0.encoded! }) | |
} | |
} | |
extension Sequence where Iterator.Element: Decodable { | |
var decoded: [Encodable] { | |
return self.filter({ $0.decoded != nil }).map({ $0.decoded! }) | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment