Skip to content

Instantly share code, notes, and snippets.

@siygle
Created July 20, 2014 14:46
Show Gist options
  • Save siygle/8eb79d7b5ba0c4df5f22 to your computer and use it in GitHub Desktop.
Save siygle/8eb79d7b5ba0c4df5f22 to your computer and use it in GitHub Desktop.
Swift Basic
// Playground - noun: a place where people can play
import UIKit
// constant and variable
//let langName: String = "Swift"
//var version: Double = 1.0
//let introduced: Int = 2014
//let isAwesome: Boolean = true
let langName = "Swift"
var version = 1.0
let introduced = 2014
let isAwesome = true
// unicode names
// String
let someString = "I appear to be a string"
// string is character
for char in "mouse" {
println(char)
}
// complex strings
let complex1 = 3, complex2 = 4
let complexResult = "\(complex1) times \(complex2) is \(complex1 * complex2)"
println(complexResult)
//array and dictionary
var names = ["Anna", "Alex", "Brain", "Jack", 4]
var numOfLegs = ["ant":6, "snake":0, "cheetah":4]
for (anName, legCount) in numOfLegs {
println("\(anName) have \(legCount) legs")
}
// optionals
//ex: int or nothing
let possilbeLegcount: Int? = numOfLegs["aardvark"]
// condition - if else
if possilbeLegcount == nil {
println("aardvark wasn't found")
} else {
let legCount: Int? = possilbeLegcount
println("aardvark has \(legCount) legs")
}
// typed collections
var names1: [String] = ["Ann", "Alex"]
// range
for num in 1...5 {
println("\(num) times 4 is \(num * 4)")
}
// function
func buildGreeting(name: String = "World") -> String {
return "Hello " + name
}
println(buildGreeting())
func refreshWebPage() -> (Int, String) {
return (200, "Success")
}
let (statusCode, statusmessage) = refreshWebPage()
println("Received \(statusCode): \(statusmessage)")
// Closures
let greetingPrinter: () -> () = {
println("Hello world!")
}
println(greetingPrinter())
// Class
class Vehicle {
var numberOfWheels = 0
var description: String {
get {
return "\(numberOfWheels) wheels"
}
}
}
var test = Vehicle()
println(test.description)
// subclass
class Bicycle: Vehicle {
init() {
super.init()
numberOfWheels = 2
}
}
let myBike = Bicycle()
println(myBike.description)
class Car: Vehicle {
var speed = 0.0
init() {
super.init()
numberOfWheels = 4
}
override var description: String {
return super.description + ", \(speed) mph"
}
}
let myCar = Car()
// willSet, didSet
class ParentCar: Car {
init() {
super.init()
}
override var speed: Double {
willSet {
if newValue > 65.0 {
println("Careful!!")
}
}
}
}
//method
class Counter {
var count = 0
func incrementBy(amount: Int) {
count += amount
}
func resetToCount(count: Int) {
self.count = count
}
}
// struct
// use struct or class?
// struct - pass by value
// class - pass by reference
struct Point {
var x, y: Double
}
// enum
enum CompassPoint {
case North, South, East, West
}
var directionToHead = CompassPoint.West
directionToHead = .East
// enum: associated values & properties
enum TrainStatus {
case OnTime
case Delayed(Int)
init() {
self = OnTime
}
var description: String {
switch self {
case OnTime:
return "on time"
case Delayed(let minutes):
return "delayed by \(minutes) minute(s)"
}
}
}
var status = TrainStatus.OnTime
status = .Delayed(42)
println(status.description)
// extensions
extension Int {
func repetitions(task: () -> ()) {
for i in 0..<self {
task()
}
}
}
5.repetitions({
println("Hello!")
})
// generic
struct Stack<T> {
var elements = [T]()
mutating func push(element: T) {
elements.append(element)
}
mutating func pop() -> T {
return elements.removeLast()
}
}
var intStack = Stack<Int>()
intStack.push(50)
let lastIn = intStack.pop()
var stringStack = Stack<String>()
stringStack.push("hello")
stringStack.push("world!")
let lastStr = stringStack.pop()
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment