Skip to content

Instantly share code, notes, and snippets.

@andriyadi
Last active September 3, 2016 06:56
Show Gist options
  • Save andriyadi/95e51b8166c4331606403c18960f9664 to your computer and use it in GitHub Desktop.
Save andriyadi/95e51b8166c4331606403c18960f9664 to your computer and use it in GitHub Desktop.
Human class in Swift, just for fun
//: Playground - noun: a place where people can play
import UIKit
import PlaygroundSupport
//PlaygroundPage.current.needsIndefiniteExecution = true
enum PhotoError: Error {
case NoURL
case BadImageData
}
class Human: CustomStringConvertible {
private(set) var father: Human?
private(set) var mother: Human?
var fullName: String?
var bornIn: String?
var bornAt: Date?
var photoUrl: URL?
var description: String {
if let name = self.fullName {
return ("\(name)")
}
else {
return ("No name")
}
}
init(name: String) {
fullName = name
}
init(name: String, fatherName: String, motherName: String) {
fullName = name;
father = Human(name: fatherName)
mother = Human(name: motherName)
}
// Async version to get the image from photoUrl
func photo(completionHandler: @escaping (UIImage?, NSError?) -> Void) {
let session = URLSession.shared
let dataTask = session.dataTask(with: self.photoUrl!) {
(data, resp, err) in
if let imgData = data {
let image = UIImage(data: imgData)
completionHandler(image, nil)
}
else {
completionHandler(nil, err as NSError?)
}
}
dataTask.resume()
}
func photo() throws -> UIImage {
guard let url = self.photoUrl else {
throw PhotoError.NoURL
}
let imageData = try Data(contentsOf: url)
guard let image = UIImage(data: imageData) else {
throw PhotoError.BadImageData
}
return image
}
func greet() -> String {
let formatter = DateFormatter()
formatter.dateFormat = "dd MMMM yyyy hh:mm"
let dateString = formatter.string(from: self.bornAt!)
let greeting = "<span>Hello world!<br/>I'm <b>\(self.fullName!)</b><br/>Born in <b>\(self.bornIn!)</b> on <b>\(dateString)</b><br/>My parents are <b>\(self.father!)</b> and <b>\(self.mother!)</b></span>"
return greeting
}
func formattedGreet() -> NSAttributedString {
let greetingString = greet() + "<style>span{font-family: 'Avenir-Roman';font-size: 12px;}</style>"
let greetingData = greetingString.data(using: String.Encoding.utf16)
let attrStr = try! NSMutableAttributedString(data: greetingData!,
options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
documentAttributes: nil)
return attrStr
}
func describeNameMeaning() -> NSAttributedString {
//assumed there's a web API to get the name's meaning :)
let meaning = "<span>My name means <b>\"My dream (is to be) the bestower of long life\"</b>.<br/><b>Alora</b> is of African origin that means <b>\"My dream\"</b>. <b>Jivanka</b> is intentionally shorted from the word of <b>\"Jivantika\"</b> which is derived from a Sanskrit word meaning <b>\"bestower of long life\"</b>. \"Alora\" is also derived from \"Elora\" which means <b>\"God is light\"</b> in Hebrew, or simply <b>\"Light\"</b> in Greek. Hence, my parents hope that someday I'll be the light and giving life to anything I'm involved in. <br/>Both Alora and Jivanka are the result of my mom's long research.<br/>My father thinks it's an awesome name because it is combination of <b>\"A\" + \"LoRa\"</b>. He always wants my name to start with \"A\". <b>\"LoRa\"</b> is a unique (and awesome) modulation technology that enable low data rate communications to be made over long distances. He is intensively doing reseach about LoRa implementation and made a few products out of it.</span><style>span{font-family: 'Avenir-Roman';font-size: 12px;}</style>"
let meaningData = meaning.data(using: String.Encoding.utf16)
let attrStr = try! NSMutableAttributedString(data: meaningData!,
options: [ NSDocumentTypeDocumentAttribute: NSHTMLTextDocumentType],
documentAttributes: nil)
return attrStr
}
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment