Skip to content

Instantly share code, notes, and snippets.

@cdl
Created September 27, 2015 05:41
Show Gist options
  • Save cdl/4a0b7423167efc7264f3 to your computer and use it in GitHub Desktop.
Save cdl/4a0b7423167efc7264f3 to your computer and use it in GitHub Desktop.
// Aiming to print out the value of a cell's text label when tapped. Assume the below
// method is inside the implementation of a UITableViewController subclass.
override func tableView(tableView: UITableView, didSelectRowAtIndexPath indexPath: NSIndexPath) {
let cell: UITableViewCell? = tableView.cellForRowAtIndexPath(indexPath)
guard let _ = cell where cell != nil else {
return
}
tableView.deselectRowAtIndexPath(indexPath, animated: true)
print("Tapped cell with label:", cell!.textLabel?.text)
}
@pcperini
Copy link

guard cell != nil else { ... }

guard is basically if but the else block has to return, break, or continue.

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