Last active
August 29, 2015 14:22
-
-
Save kurtisdunn/da9e93fa3d723dedc995 to your computer and use it in GitHub Desktop.
Quick cheat sheet of common Swift vars, arrays, funcs, classes
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// Kurt's Swift guide. | |
import UIKit | |
// Variables | |
// 'var' will pick a type ie string or int or whatever | |
var make = "Ferrari" | |
var model = "488 GTB" | |
// Old concatenation | |
var returnValue = make + " " + model | |
// New concatenation | |
var retunVal = "You have a \(make) \(model)" | |
// Force a var type | |
var type:String = "Coupe" | |
var cylinders:Int = 8 | |
// concatenation in new type with multiplier | |
var score = 125 | |
var multi = 4 | |
var output = "Your score is \(score * multi)" | |
//let is a fixed variable that cannot change | |
let player = "Kurt" | |
let MY_SITE = "http://www.this.com" | |
//Arrays | |
var cars = ["Ford", "Ferrari", "Fiat", "BMW", "Audi"] | |
var car0 = cars[0] | |
// Each for loop | |
for car in cars{ | |
//println(car) | |
} | |
//Traditional for loop | |
for (var i = 0; i < 3; ++i){ | |
println(cars[i]) | |
} | |
//conditionals | |
var fruit = "apple" | |
var myFruit = "mango" | |
// If/Else | |
if(fruit == myFruit){ | |
println("This is true") | |
}else if(fruit == "apple"){ | |
println("") | |
println("true") | |
}else{ | |
println("this is false") | |
} | |
// Switch | |
switch fruit{ | |
case "apple": | |
println("the fruit was an \(fruit)") | |
case "bannana": | |
println("the fruit is a \(fruit)") | |
default: | |
println("nothing") | |
} | |
// Functions | |
func add(num1:Int, num2:Int) -> Int{ | |
return num1 + num2 | |
} | |
add(50, 4) | |
func getCars() -> (String, String){ | |
return ("488 GTB", "458 Speciale") | |
} | |
getCars(); | |
//Classes | |
class Cars{ | |
var doors = 0 | |
var cylinders = 0 | |
func getDetails() -> String { | |
return "This car has \(doors) doors and \(cylinders) cylinders" | |
} | |
} | |
var myCar = Cars() | |
myCar.doors = 2 | |
myCar.cylinders = 8 | |
myCar.getDetails() | |
class Animal{ | |
var name = "default" | |
var age = 0 | |
func getDetails() -> String { | |
return "this animals name is \(name) it is \(age) years old." | |
} | |
} | |
var myAnimal = Animal() | |
myAnimal.name = "Ralphy" | |
myAnimal.age = 10 | |
myAnimal.getDetails() | |
class Dog : Animal{ | |
func woof() -> String { | |
return "Woof Woof Woof." | |
} | |
} | |
class Cat : Animal { | |
func meow() -> String { | |
return "Cats make me sneeze!" | |
} | |
} | |
var kurtsDog = Dog() | |
kurtsDog.name = "Ralphy" | |
kurtsDog.age = 10 | |
kurtsDog.woof() | |
kurtsDog.getDetails() | |
var kurtsCat = Cat() | |
kurtsCat.name = "Frank" | |
kurtsCat.age = 1 | |
kurtsCat.meow() | |
kurtsCat.getDetails() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment