Create a gist now

Instantly share code, notes, and snippets.

What would you like to do?
Swift Playground
//: Playground - noun: a place where people can play
import UIKit
// Strings and string concatenation
let baseLabel = "Base"
let baseNumber = 67
let concatStr1 = baseLabel + " " + String(baseNumber)
print(concatStr1)
let concatStr2 = "\(baseLabel) \(baseNumber)"
print(concatStr2)
let cat = "🐱"
let πŸΆπŸ’© = "πŸ™ˆ πŸ™‰ πŸ™Š"
print(cat + πŸΆπŸ’©)
// Looping and lists
let names = ["Tom", "Dick", "Harry"]
/*for (var i = 0 ; i < names.count ; i++) {
println(names[i])
}*/
// Functions
func drinkOrder(firstName:String, drink:String) -> String {
return "\(firstName) ordered his favorite drink \(drink)"
}
drinkOrder("Tom", drink:"Coffee")
// First-class functions
func hasAnyMatches (list: [Int], condition: Int -> Bool) -> Bool {
for item in list {
if condition(item) {
return true;
}
}
return false;
}
func lessThanZero (value:Int) -> Bool {
return value < 0
}
var numbers = [12, -1, 5, -3, -33, 178]
hasAnyMatches(numbers, condition: lessThanZero)
// Classes
enum CardClass {
case Common, Rare, Epic, Legendary, Promo
func simpleDescription() -> String {
switch self {
case .Common:
return "Common"
case .Rare:
return "Rare"
case .Epic:
return "Epic!"
case .Legendary:
return "Legendary!!!"
case .Promo:
return "Promotional"
}
}
}
class HearthstoneCard {
var cardClass:CardClass
init() {
cardClass = CardClass.Common
}
func simpleDescription() -> String {
return "A Hearthstone \(cardClass.simpleDescription()) card"
}
}
var c = HearthstoneCard()
print(c.simpleDescription())
// Integer overflow?
//var a:CInt = 2147483647
//a+=6
// Buffer?
//let inputSize : Int32 = getchar()
//let buf = UnsafeMutablePointer<Int8>.alloc(Int(inputSize))
//gets(buf)
@mchow01
Owner
mchow01 commented Jan 26, 2016

Updated to Swift 2.1

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment