Skip to content

Instantly share code, notes, and snippets.

@nerdEd
Created July 29, 2014 20:22
Show Gist options
  • Save nerdEd/00abc59bc96848b732ba to your computer and use it in GitHub Desktop.
Save nerdEd/00abc59bc96848b732ba to your computer and use it in GitHub Desktop.
Swift Talk Code
// Playground - noun: a place where people can play
import Cocoa
// Immutable
let apiKey = "12lkj1llk1j291lnk12el"
// Mutable
var changingValue = "I might change!"
// Inferred type
let someVariable = "this is a string, right?"
// Type annotation
var someOtherVariable: String
// Optional
var postalCodeExtension: String?
// Implicitly unwrapped optional
var postalCode: String!
var zip: String!
postalCodeExtension = "1234"
postalCode = "21231"
if(postalCodeExtension) {
zip = "\(postalCode)-\(postalCodeExtension!)"
} else {
zip = postalCode
}
zip
func someFunction() -> String? {
return nil
}
if let someConstant = someFunction() {
// use someConstant
} else {
// handle the case where someConstant is nil
}
class Locker {
var combination: String
init(combo: String) {
combination = combo
}
}
class Athlete {
var locker: Locker?
}
var person = Athlete()
// Runtime error
person.locker!.combination
// Returns String? with value of nil
person.locker?.combination
// void function
func iDoNothing() {
}
// return type String
func stringMaker() -> String {
return "some string"
}
// return type optional String
func potentialStringMaker() -> String? {
let random = arc4random_uniform(2)
return random == 1 ? "A String!" : nil
}
// not required
func changeCase(someString: String) -> String {
return someString.uppercaseString
}
changeCase("whatever")
// required
func pleaseChangeCase(beingChanged someString: String) -> String {
return someString.uppercaseString
}
pleaseChangeCase(beingChanged: "whatever")
// shorthand for required
func changeMyCase(#beingChanged: String) -> String {
return beingChanged.uppercaseString
}
changeMyCase(beingChanged: "whatever")
func sayHello(name: String = "Guest") -> String {
return "Hello, \(name)"
}
sayHello() // "Hello, Guest"
sayHello(name: "frank") // "Hello, frank"
func swapInts(inout a: Int, inout b: Int) {
let tempA = a
a = b
b = tempA
}
var someInt = 3
var anotherInt = 4
swapInts(&someInt, &anotherInt)
class Car {
}
extension Car: Automobile {
func moveForward() {
}
}
protocol Automobile {
func moveForward()
}
protocol Mover {
func move() -> Void
}
protocol Shaker {
func shake() -> Void
}
func dance(person: protocol<Mover, Shaker>) -> Void {
person.move()
person.shake()
}
let sortingClosure = {(num1: Int, num2: Int) -> Bool in
return num1 > num2
}
func doStuff(success: (msg: String) -> Void, fail: (errorCode: String, msg: String) -> Void) {
success(msg: "The thing worked!")
fail(errorCode: "500", msg: "The thing did not work")
}
doStuff(
{(msg: String) in
println(msg)
},
{(errorCode: String, msg: String) in
println(msg)
println(errorCode)
}
)
class Human {
var firstName: String
var lastName: String
// optional property
var petName: String?
// property w/ default value
var location: String = "Baltimore"
// computed property
var fullName: String {
get {
return "\(firstName) \(lastName)"
}
}
// lazy initialized property
lazy var lawyer = Lawyer()
init(first: String, last: String) {
firstName = first
lastName = last
}
}
class Lawyer: Human {
init() {
super.init(first: "Lionel", last: "Hutz")
}
}
class MileageTracker {
var distanceTraveled: Int = 0 {
willSet(newMileage) {
println("Wow \(newMileage) is a lot of miles")
}
didSet {
if(distanceTraveled < oldValue) {
distanceTraveled = oldValue
}
}
}
}
var m = MileageTracker()
m.distanceTraveled = -1
m.distanceTraveled = 5
public class Thing {
// Can only be used inside this class
private var internalProperty: String = ""
// Internal access is default
let someThing = "Whatever"
// Can be used outside of this module
public var giraffeCount: Int = 10
// Getter is Internal setter is Private
private(set) var something: String = "is a thing?"
// Getter is Public setter is Internal
internal(set) var anotherThing: String = "this is a thing."
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment