Skip to content

Instantly share code, notes, and snippets.

View runkaiz's full-sized avatar
🏠
Working from home

Runkai Zhang runkaiz

🏠
Working from home
View GitHub Profile
@runkaiz
runkaiz / Person.swift
Created November 17, 2019 21:21
Having Fun With DIY Objects in Swift
// 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 {
@runkaiz
runkaiz / Person-Beginning.swift
Created November 18, 2019 02:50
Start with the basic setup
// 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?
@runkaiz
runkaiz / Person-Hashable.swift
Last active November 18, 2019 02:55
Person Hashable
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)
}
@runkaiz
runkaiz / Person-Init.swift
Created November 18, 2019 02:55
Person Initialization.
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
}
@runkaiz
runkaiz / john.swift
Last active November 18, 2019 02:58
John
let John = Person(name: "John", birthday: "03/02/1985", sex: "Female", relatives: nil, uniId: "John3srd")
@runkaiz
runkaiz / james.swift
Created November 18, 2019 02:58
James
let James = Person(name: "James", birthday: "06/22/2009", sex: "Male", relatives: [John:"Parent"], uniId: "James1st")
@runkaiz
runkaiz / add-relative.swift
Created November 18, 2019 02:59
Adding relatives.
func addRelative(_ person: Person, relation: String) {
self.relatives?[person] = relation
}
@runkaiz
runkaiz / Add John
Created November 18, 2019 03:00
Adding John.
John.addRelative(James, relation: "child")
let John = Person(name: "John", birthday: "03/02/1985", sex: "Femle", relatives: nil, uniId: "John3srd")
enum sexType {
case female
case male
}