Skip to content

Instantly share code, notes, and snippets.

@BradB132
BradB132 / GemsOfSwift.swift
Created May 31, 2015 14:38
FirstAppInSwift-3
func exampleFunc(description intArg:Int) {
// ...
}
exampleFunc(description: 5)
@BradB132
BradB132 / GemsOfSwift.swift
Created May 31, 2015 14:38
FirstAppInSwift-4
func exampleFunc(# intArg:Int) {
// ...
}
exampleFunc(intArg: 5)
@BradB132
BradB132 / GemsOfSwift.swift
Created May 31, 2015 14:39
FirstAppInSwift-5
func add(first:Int, second:Int) -> Int {
return first+second
}
add(4, second: 5)
@BradB132
BradB132 / GemsOfSwift.swift
Created May 31, 2015 14:39
FirstAppInSwift-6
func add(first:Int, _ second:Int) -> Int {
return first+second
}
add(4, 5)
@BradB132
BradB132 / GemsOfSwift.swift
Created May 31, 2015 14:40
FirstAppInSwift-7
func defaultParameters(myArg:Int = 0) {
// ...
}
defaultParameters() //passing nothing gives 'myArg' a value of 0
defaultParameters(myArg: 5) //'myArg' will be 5
@BradB132
BradB132 / GemsOfSwift.swift
Last active August 29, 2015 14:22
FirstAppInSwift-8
public class MyClass {
private(set) public var myProperty:Int = 0
}
//Here’s some code that tests our new property
let myObj = MyClass()
println(myObj.myProperty) //this line compiles
myObj.myProperty = 5 //this causes a compile error
@BradB132
BradB132 / TableDelegate.swift
Created May 31, 2015 14:42
FirstAppInSwift-9
func tableView(tableView: UITableView, editActionsForRowAtIndexPath indexPath: NSIndexPath) -> [AnyObject]? {
var myAction = UITableViewRowAction(style: .Default, title: "My Custom Action", handler: { (action:UITableViewRowAction!, indexPath:NSIndexPath!) -> Void in
//block that runs when 'My Custom Action' button is tapped
})
//it's useful to differentiate each button with color
myAction.backgroundColor = UIColor.lightGrayColor()
//create additional actions here
@BradB132
BradB132 / MyController.swift
Created May 31, 2015 14:43
FirstAppInSwift-10
let alert = UIAlertController(title: "Title", message: "Message", preferredStyle: .Alert /* or .ActionSheet*/)
alert.addAction(UIAlertAction(title: "OK", style: .Default, handler: { (action:UIAlertAction!) in
//this block runs when 'OK' is tapped
}))
alert.addAction(UIAlertAction(title: "Cancel", style: .Cancel, handler: { (action:UIAlertAction!) in
//this block runs when 'Cancel' is tapped
}))
@BradB132
BradB132 / MyController.swift
Created May 31, 2015 14:43
FirstAppInSwift-11
let alertStyle:UIAlertControllerStyle = UIDevice.currentDevice().userInterfaceIdiom == .Phone ? .ActionSheet : .Alert
@BradB132
BradB132 / MyController.swift
Created May 31, 2015 14:44
FirstAppInSwift-12
table.estimatedRowHeight = 44;
table.rowHeight = UITableViewAutomaticDimension