Skip to content

Instantly share code, notes, and snippets.

Avatar

Alex Curran amlcurran

View GitHub Profile
View PretendAPI.txt
This is some text downloaded from an API!
View ConstraintsExample.swift
func doStuff() {
keyInput.constrain(toSuperviewSafeArea: .top, .leading, insetBy: 16)
blurView.contentView.addSubview(valueInput)
valueInput.constrain(toSuperviewSafeArea: .trailing, .bottom, insetBy: 16)
verticalLayoutConstraint = keyInput.constrain(.bottom, to: valueInput, .top, withOffset: -8)
verticalLayoutConstraint.priority = .defaultHigh
verticalLayoutConstraint.isActive = false
sameWidthConstraint = keyInput.constrain(.width, to: valueInput, .width)
sameTopConstraint = keyInput.constrain(.top, to: valueInput, .top)
View UIView+Constraints.swift
import UIKit
extension UIView {
func constrain(toSuperview edges: NSLayoutAttribute..., insetBy inset: CGFloat = 0) {
for edge in edges {
constrain(toSuperview: edge, withOffset: offset(for: edge, ofInset: inset))
}
}
View Foo.swift
func displayErrorMessage(_ optionalMessage: String?) {
if let message = optionalMessage {
print(message)
}
}
displayErrorMessage("Something went wrong") // Prints out "Something went wrong"
displayErrorMessage(nil) // doesn't do anything
View combined-report
#!/bin/bash
if hash lines 2>/dev/null ; then
if [ -f imports.txt -o -f lines.txt ]; then
echo "Previous import.txt and/or lines.txt exist! Delete them first."
exit
fi
imports > imports.txt && lines > lines.txt
open imports.txt lines.txt
@amlcurran
amlcurran / Completion.swift
Last active May 21, 2018
Better completion blocks by using higher order functions
View Completion.swift
func completion<Result>(onResult: @escaping (Result) -> Void, onError: @escaping (Error) -> Void) -> ((Result?, Error?) -> Void) {
return { (maybeResult, maybeError) in
if let result = maybeResult {
onResult(result)
} else if let error = maybeError {
onError(error)
} else {
onError(SplitError.NoResultFound)
}
}
View fruits.json
{
"fruits" : [
{
"name" : "Apple",
"image" : "https://upload.wikimedia.org/wikipedia/commons/thumb/1/15/Red_Apple.jpg/265px-Red_Apple.jpg",
"price" : 35
},
{
"name" : "Banana",
"image" : "https://upload.wikimedia.org/wikipedia/commons/thumb/4/44/Bananas_white_background_DS.jpg/320px-Bananas_white_background_DS.jpg",
View After.swift
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if recipes[indexPath.row].isPromoted {
let promotedCell = tableView.dequeueReusableCell(withIdentifier: "promoted", for: indexPath) as! PromotedRecipeCell
promotedCell.recipe = recipe
promotedCell.promotion = recipe.promotion // we've guarded here using the isPromoted method, so the value is unwrapped
return promotedCell
} else {
let normalCell = tableView.dequeueReusableCell(withIdentifier: "normal", for: indexPath) as! RecipeCell
normalCell.recipe = recipe
return normalCell
View Before.swift
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
if let promotion = recipes[indexPath.row].promotion {
let promotedCell = tableView.dequeueReusableCell(withIdentifier: "promoted", for: indexPath) as! PromotedRecipeCell
promotedCell.recipe = recipe
promotedCell.promotion = promotion
return promotedCell
} else {
let normalCell = tableView.dequeueReusableCell(withIdentifier: "normal", for: indexPath) as! RecipeCell
normalCell.recipe = recipe
return normalCell
@amlcurran
amlcurran / Optional+Fluent.swift
Created Sep 4, 2016
Some fluent wrappers around the nil-coalescing operator and optionals in Swift
View Optional+Fluent.swift
extension Optional {
func or(_ backup: Wrapped) -> Wrapped {
if let wrapped = self {
return wrapped
}
return backup
}
}
You can’t perform that action at this time.