Skip to content

Instantly share code, notes, and snippets.

View grago's full-sized avatar

Gustavo Rago grago

View GitHub Profile
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
@IBOutlet weak var tableView: UITableView!
override func viewDidLoad() {
super.viewDidLoad()
NSLayoutConstraint.activate([
tableView.topAnchor.constraint(equalTo: view.topAnchor),
@beccadax
beccadax / Example.swift
Last active March 6, 2020 16:10
Elegant handling of localizable strings in Swift. Note: This code is in Swift 2 and would need updates to be used in modern Swift.
let color = "blue"
let num = 42
localized("Colorless green ideas sleep furiously.")
localized("Colorless \(color) ideas sleep furiously.")
localized("\(num.formatted("%05d")) colorless green ideas sleep furiously.")
@plumhead
plumhead / StringSize.swift
Created September 15, 2015 13:34
String extension to find the layout size of a String with specified attributes.
extension String {
func size(withAttributes attrs: [String:AnyObject], constrainedTo box: NSSize) -> NSRect {
let storage = NSTextStorage(string: self)
let container = NSTextContainer(containerSize: NSSize(width: box.width, height: box.height))
let layout = NSLayoutManager()
layout.addTextContainer(container)
storage.addLayoutManager(layout)
storage.addAttributes(attrs, range: NSMakeRange(0, storage.length))
container.lineFragmentPadding = 0.0
let _ = layout.glyphRangeForTextContainer(container)
@chriseidhof
chriseidhof / json.swift
Last active March 21, 2019 07:45
Reflection
import Cocoa
struct Person {
var name: String = "John"
var age: Int = 50
var dutch: Bool = false
var address: Address? = Address(street: "Market St.")
}
struct Address {
import Foundation
protocol Serializable {
static func deserializeInto(bytePtr: UnsafeMutablePointer<UInt8>, bytes: ArraySlice<UInt8>) -> ArraySlice<UInt8>
}
extension Serializable {
typealias WorkaroundSelf = Self
@tonyarnold
tonyarnold / FirstResponderRAC.m
Last active August 29, 2015 14:21
Detects when a text field's current editor is the parent window's first responder (the "view" here is an NSTableCellView subclass)
RACSignal *textFieldDidFocus = [[[[view rac_valuesForKeyPath:@keypath(view, textField.window.firstResponder) observer:view]
takeUntil:view.rac_willDeallocSignal] filter:^BOOL(id value) {
@strongify(view);
return [view.textField.currentEditor isEqualTo:value];
}]
mapReplace:view.textField];
@alskipp
alskipp / filter_colors.hs
Last active January 6, 2024 07:23
Filtering arrays of enums (Haskell / Swift 1.2 / Swift 2.0 comparison)
-- data structure for RGBA, RGB & CMYK colors
data Color = RGBA Float Float Float Float
| RGB Float Float Float
| CMYK Float Float Float Float
deriving (Eq, Ord, Show)
-- list of colors
colors = [ RGBA 0.1 0.1 0.5 0.2
, RGBA 1 1 0 0.9
, RGB 0.5 0.5 0
@JaviLorbada
JaviLorbada / FRP iOS Learning resources.md
Last active April 8, 2024 18:07
The best FRP iOS resources.

Videos