Skip to content

Instantly share code, notes, and snippets.

@loganmoseley
loganmoseley / PSTCollectionView.contentSize
Created January 21, 2013 19:44
pstCollectionView.contentSize.height == 431, while uiCollectionView.contentSize.height == 162.5. See http://i6.minus.com/jqrf8if9AEwDl.png for result.
iPhone 6.0 Simulator
(lldb) po self.collectionView
(PSTCollectionView *) $1 = 0x12a13a00 <UICollectionView: 0x12a13a00; frame = (0 0; 320 431); clipsToBounds = YES; autoresize = W+H; gestureRecognizers = <NSArray: 0x1216de90>; layer = <CALayer: 0x1216db60>; contentOffset: {0, -64}> collection view layout: <PSUICollectionViewFlowLayout_: 0x12173d00>
(lldb) p (UIEdgeInsets) [self.collectionView contentInset]
(UIEdgeInsets) $2 = {
(CGFloat) top = 64
(CGFloat) left = 0
(CGFloat) bottom = 0
@loganmoseley
loganmoseley / UIView+RecursiveDescription.swift
Last active December 11, 2015 20:17
Concatenate strings in reduce() with shorthand syntax "+"
//
// UIView+RecursiveDescription.swift
//
// Created by Logan Moseley on 12/11/15.
//
import UIKit
extension UIView {
var recursiveDescription: String {
@loganmoseley
loganmoseley / UserDefaultsKVOTest.swift
Last active August 28, 2016 21:04
Tests the interaction of KVO and resetStandardUserDefaults().
/**
Key-Value Observing of NSUserDefaults.standardUserDefaults is unsafe.
Do not KVO the standardUserDefaults because an invocation of resetStandardUserDefaults()
anywhere in the codebase causes that KVO to silently stop observing. The comment of
resetStandardUserDefaults() in NSUserDefaults.h (below) states this behavior, but I'd
have found the comment more clear had it pointed out that the behavior makes
standardUserDefaults unfit for Key-Value Observation.
> The only visible effect this has is that all KVO observers of the previous
@loganmoseley
loganmoseley / Protocol Misunderstanding.swift
Created October 24, 2016 01:16
Demonstration of property name collision
// Person.swift
struct Person {
let givenName: String
let familyName: String
}
// SecretAgent.swift
protocol SecretAgent {
var givenName: String { get }
}
@loganmoseley
loganmoseley / Functor.swift
Created April 25, 2017 17:05
Type erase fmap to make a Functor
// MARK: Compose
func compose<A,B,C>(_ f: @escaping (B) -> C, after g: @escaping (A) -> B) -> ((A) -> C) {
return { f(g($0)) }
}
// MARK: Functor
/// The FunctorType protocol is used for types that can be mapped over. Types conforming to
/// FunctorType must implement a Functor constructor which must satisfy the following laws:
//
// CDZIdioms.h
// https://www.dzombak.com/blog/2015/02/Tiny-Swift-idioms-in-ObjC.html
//
// Created by Chris Dzombak on 3/21/15.
// Copyright (c) 2015 Chris Dzombak. All rights reserved.
//
#ifndef CDZIdioms_h
#define CDZIdioms_h
import Foundation
import Result
public extension Result {
/// Constructs a success wrapping `value`, iff `value` is not nil and `error` is nil.
///
/// Constructs a failure wrapping `error`, iff `error` is not nil and `value` is nil.
///
/// Otherwise, returns nil.
@loganmoseley
loganmoseley / UIButton+BlockAction.swift
Created November 21, 2017 22:20
Add a block as the touchUpInside handler.
import UIKit
public extension UIButton {
public typealias TouchUpInside = () -> Void
public func setTouchUpInside(_ block: @escaping TouchUpInside) {
touchUpInsideBlock = block
addTarget(self, action: #selector(runTouchUpInside), for: .touchUpInside)
}
@loganmoseley
loganmoseley / remove-fileheader.swift
Created January 8, 2018 19:31
Remove headers from all .c .h .m and .swift templates in Xcode.
#!/usr/bin/swift
/// Args:
/// --xcode {XCODE_PATH}
import Foundation
extension Array {
func paired() -> [(Element, Element)] {
return stride(from: 0, to: count - 1, by: 2).map {
(self[$0], self[$0 + 1])
@loganmoseley
loganmoseley / ContainsSwitch.swift
Created January 26, 2018 00:27
Using `switch` to find a substring.
import Cocoa
struct Contains : ExpressibleByStringLiteral {
let s: String
static func ~= (l: Contains, r: Contains) -> Bool {
return r.s.contains(l.s)
}
init(_ c: String) {