Skip to content

Instantly share code, notes, and snippets.

View amlcurran's full-sized avatar

Alex Curran amlcurran

View GitHub Profile
@amlcurran
amlcurran / Optional+Fluent.swift
Created September 4, 2016 21:32
Some fluent wrappers around the nil-coalescing operator and optionals in Swift
extension Optional {
func or(_ backup: Wrapped) -> Wrapped {
if let wrapped = self {
return wrapped
}
return backup
}
}
// 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();
}
@amlcurran
amlcurran / Ifguardlet.swift
Created July 31, 2016 13:55
if let and guard let
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
@amlcurran
amlcurran / Chaining.swift
Created July 31, 2016 13:36
swift-optional-chaining
let optionalString: String? = nil
let count = optionalString?.utf8.count // count is an optional Int
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
@amlcurran
amlcurran / delegation.swift
Last active June 12, 2016 12:59
Delegation 3: Delegation
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)
@amlcurran
amlcurran / extension.swift
Created June 12, 2016 12:58
Delegation 2: extension
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
@amlcurran
amlcurran / inheritance.swift
Created June 12, 2016 12:58
Delegation 1: Inheritance
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
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
<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;