Skip to content

Instantly share code, notes, and snippets.

@erica erica/fortune.swift

Created Apr 14, 2015
Embed
What would you like to do?
Fortune script in Swift. Make sure to chmod +x before using.
#!/usr/bin/env xcrun swift
import Foundation
func Fortune() -> String? {
if let urlString = "http://iheartquotes.com/api/v1/random?format=json".stringByAddingPercentEscapesUsingEncoding(NSUTF8StringEncoding),
url = NSURL(string:urlString),
data = NSData(contentsOfURL: url),
json = NSJSONSerialization.JSONObjectWithData(data, options: NSJSONReadingOptions(0), error: nil) as? NSDictionary,
quote = json["quote"] as? String {
return quote
}
return nil
}
println(Fortune() ?? "Sorry!")
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
You can’t perform that action at this time.