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
| // If you are not familiar with Swift, the Foundation framework is the basic support structure (hence the name Foundation) that allows us to do a lot of things in Swift. | |
| import Foundation | |
| enum sexType { | |
| case female | |
| case male | |
| } | |
| class Person: Hashable { | |
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
| // If you are not familiar with Swift, the Foundation framework is the basic support structure (hence the name Foundation) that allows us to do a lot of things in Swift. | |
| import Foundation | |
| class Person { | |
| // Here we can list out the properties we are going to store about a person. | |
| // We would want some value to be optional just in case when we want to save an incomplete profile. | |
| var name: String? | |
| // I made the birthday value as a string so it is flexible to convert and print. | |
| var birthday: String? | |
| // This is going to be changed since it is not a numerical value. | |
| var sex: String? |
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
| class Person: Hashable { | |
| // … the rest of the code remains the same. | |
| // We want to make sure the Person object can be used as a key in a dictionary. The Unique Identifier is used here to help with that. | |
| static func == (lhs: Person, rhs: Person) -> Bool { | |
| // Here we are using the unique identifier to generate hash. | |
| return lhs.uniId == rhs.uniId | |
| } | |
| func hash(into hasher: inout Hasher) { | |
| hasher.combine(self.uniId) | |
| } |
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
| init(name: String?, birthday: String?, sex: String?, relatives: [Person:String]?, uniId: String) { | |
| self.name = name | |
| self.birthday = birthday | |
| self.sex = sex | |
| self.relatives = relatives | |
| self.uniId = uniId | |
| } |
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
| let John = Person(name: "John", birthday: "03/02/1985", sex: "Female", relatives: nil, uniId: "John3srd") |
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
| let James = Person(name: "James", birthday: "06/22/2009", sex: "Male", relatives: [John:"Parent"], uniId: "James1st") |
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
| func addRelative(_ person: Person, relation: String) { | |
| self.relatives?[person] = relation | |
| } |
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
| John.addRelative(James, relation: "child") |
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
| let John = Person(name: "John", birthday: "03/02/1985", sex: "Femle", relatives: nil, uniId: "John3srd") |
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
| enum sexType { | |
| case female | |
| case male | |
| } |
OlderNewer