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
extension Optional { | |
func or(_ backup: Wrapped) -> Wrapped { | |
if let wrapped = self { | |
return wrapped | |
} | |
return backup | |
} | |
} |
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
// this is less descriptive and more difficult to read | |
if (user.getState() == LoginState.LOGGED_IN && articleList.getQueue().size() > 0) { | |
uploadArticles(); | |
} | |
// this is much easier to read and know what's happening | |
if (user.isLoggedIn() && articleList.hasQueuedArticles()) { | |
uploadArticles(); | |
} |
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
func doSomething(optionalString: String?) { | |
if let string = optionalString { | |
let count = string.utf8.count // string is not optional and can be used normally | |
} | |
} | |
func doSomething(optionalString: String?) { | |
guard let string = optionalString else { | |
// this closure is called if optionalString was nil | |
return |
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
let optionalString: String? = nil | |
let count = optionalString?.utf8.count // count is an optional Int |
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
var string: String = "hello" | |
string = nil // fails to compile - trying to assign nil to a non-optional value | |
var optionalString: String? = "hello" | |
optionalString = nil // compiles fine, as we've declared this to be an optional string |
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
class FirstViewController: UIViewController { | |
private let items : [String] = [ /* some items */] | |
private let tableView = UITableView() | |
private var delegate : UITableViewDelegate! | |
/* lots of other methods */ | |
override func viewDidAppear(animated: Bool) { | |
super.viewDidAppear(animated) |
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
class FirstViewController: UIViewController { | |
private let items : [String] = [ /* some items */] | |
private let tableView = UITableView() | |
/* lots of other methods */ | |
override func viewDidAppear(animated: Bool) { | |
super.viewDidAppear(animated) | |
tableView.delegate = self |
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
class FirstViewController: UIViewController, UITableViewDelegate { | |
private let items : [String] = [ /* some items */] | |
private let tableView = UITableView() | |
/* lots of other methods */ | |
override func viewDidAppear(animated: Bool) { | |
super.viewDidAppear(animated) | |
tableView.delegate = self |
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
class FirstViewController: UIViewController, UITableViewDelegate { | |
private let items : [String] = [ /* some items */] | |
private let tableView = UITableView() | |
/* lots of other methods */ | |
override func viewDidAppear(animated: Bool) { | |
super.viewDidAppear(animated) | |
tableView.delegate = self |
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
<link rel="import" href="../core-icon-button/core-icon-button.html"> | |
<link rel="import" href="../core-toolbar/core-toolbar.html"> | |
<link rel="import" href="../core-header-panel/core-header-panel.html"> | |
<polymer-element name="my-element"> | |
<template> | |
<style> | |
:host { | |
position: absolute; |