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-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-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.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 {