Skip to content

Instantly share code, notes, and snippets.

Embed
What would you like to do?
Experiments in typing
//: Playground - noun: a place where people can play
import UIKit
protocol HasCreatedAtTimestamp {
var createdAt: Date { get }
func setCreatedAt(date: Date) -> Self
}
struct Address: HasCreatedAtTimestamp {
let city: String
let state: String
let code: String
let country: String
let createdAt: Date
init(city: String, state: String, code: String, country: String, createdAt: Date) {
self.city = city
self.state = state
self.code = code
self.country = country
self.createdAt = createdAt
}
func setCreatedAt(date: Date) -> Address {
return Address(city: city, state: state, code: code, country: country, createdAt: date)
}
}
struct Person: HasCreatedAtTimestamp {
let name: String
let createdAt: Date
init(name: String, createdAt: Date) {
self.name = name
self.createdAt = createdAt
}
func setCreatedAt(date: Date) -> Person {
return Person(name: name, createdAt: date)
}
}
extension Array where Element: HasCreatedAtTimestamp {
func overrideCreateAt(with date: Date) -> Array<Element> {
return self.map({ (s) -> Element in
return s.setCreatedAt(date: date)
})
}
}
func save(addresses: Array<Address>) {
let updatedAddresses = addresses.overrideCreateAt(with: Date())
print(updatedAddresses[0].createdAt)
}
func save(persons: Array<Person>) {
let updatedPersons = persons.overrideCreateAt(with: Date())
print(updatedPersons[0].createdAt)
}
let persons = [
Person(name: "Erin", createdAt: Date(timeIntervalSinceNow: -3600.0)),
Person(name: "Rudy", createdAt: Date(timeIntervalSinceNow: -7200.0))
]
print(persons[0].createdAt)
save(persons: persons)
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment