Created
April 5, 2016 18:02
-
-
Save speaktoalvin/aef4f339d4339931cb825ac7f11e2be5 to your computer and use it in GitHub Desktop.
Creating a Custom class in Swift - For converting to NSData
This file contains hidden or 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 UIKit | |
| //MARK: MultiPeer_CustomClass | |
| class MultiPeer_CustomClass : NSObject | |
| { | |
| //MARK: Local Variables | |
| var name : String? | |
| var age : Int? | |
| var email : String? | |
| // Decode | |
| required convenience init(coder decoder: NSCoder) | |
| { | |
| self.init() | |
| if let proper_name = decoder.decodeObjectForKey("name") as? String | |
| { | |
| self.name = proper_name | |
| } | |
| if let proper_age = decoder.decodeObjectForKey("age") as? Int | |
| { | |
| self.age = proper_age | |
| } | |
| if let proper_email = decoder.decodeObjectForKey("email") as? String | |
| { | |
| self.email = proper_email | |
| } | |
| } | |
| // Init | |
| convenience init(name : String, age : Int, email : String) | |
| { | |
| self.init() | |
| self.name = name | |
| self.age = age | |
| self.email = email | |
| } | |
| // Encode | |
| func encodeWithCoder(coder : NSCoder) | |
| { | |
| if let proper_name = self.name | |
| { | |
| coder.encodeObject(proper_name, forKey: "name") | |
| } | |
| if let proper_age = self.age | |
| { | |
| coder.encodeObject(proper_age, forKey: "age") | |
| } | |
| if let proper_email = self.email | |
| { | |
| coder.encodeObject(proper_email, forKey: "email") | |
| } | |
| } | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment