Skip to content

Instantly share code, notes, and snippets.

@SlaunchaMan
SlaunchaMan / KVO.swift
Created June 7, 2017 15:44
Attempting to use the new block-based KVO in Xcode 9 and Swift 4
//: Playground - noun: a place where people can play
import Foundation
class HelloThere: NSObject {
@objc var value: String = ""
}
@SlaunchaMan
SlaunchaMan / ugh.swift
Last active May 26, 2017 16:24
Hashing in a Mock Subclass of an NSObject
override var hashValue: Int {
var mutableSelf = self
return withUnsafePointer(to: &mutableSelf) { (ptr) -> Int in
return ptr.withMemoryRebound(to: Int.self, capacity: 1, {
return $0.pointee
})
}
}
#import <XCTest/XCTest.h>
NS_ASSUME_NONNULL_BEGIN
@interface XCTestCase (FTTObjectDestructionWaiter)
- (void)waitForDestructionOfObject:(id)anObject;
- (void)waitForDestructionOfObject:(id)anObject timeout:(NSTimeInterval)timeout;
@end
@SlaunchaMan
SlaunchaMan / TestQueueing.swift
Created May 1, 2017 14:48
Helper methods for asynchronous tests
import Foundation
import Dispatch
import XCTest
extension DispatchQueue {
func asyncAfter(_ seconds: TimeInterval,
execute work: @escaping @convention(block) () -> Void) {
asyncAfter(deadline: DispatchTime.now() + seconds, execute: work)
}
@SlaunchaMan
SlaunchaMan / nnnn-nil-values-for-objective-c-primitives.md
Created February 18, 2017 03:33
Swift-Evolution Draft Proposal: nil Values for Objective-C Primitives

nil Values for Objective-C Primitives

Introduction

This proposal aims to improve Swift’s compatibility with Objective-C by allowing Objective-C methods to declare nullability for primitives by using sentinel values. By using the format nullable(value), the syntax will stay close to existing Objective-C nullability specifiers, as well as serving as inline documentation about the method’s behavior.

@SlaunchaMan
SlaunchaMan / FizzBuzz.swift
Created October 31, 2016 14:10
A quick FizzBuzz I wrote to show pattern matching in Swift’s switch statement.
let result = (1 ... 100).map { i -> String in
switch (i % 3 == 0, i % 5 == 0) {
case (true, true):
return "FizzBuzz"
case (true, false):
return "Fizz"
case (false, true):
return "Buzz"
default:
return "\(i)"
@SlaunchaMan
SlaunchaMan / HomeButtonPositioning.swift
Created June 21, 2016 03:05
Pinning a UIView to the Home button side of its superview in Swift 3
import UIKit
extension UIView {
public func constraintsForPinningToHomeButton(withSpacing spacing: CGFloat = 0.0) -> [NSLayoutConstraint] {
guard let superview = superview else { return [] }
switch (UIDevice.current().orientation, UIApplication.shared().statusBarOrientation) {
case (.landscapeLeft, .landscapeRight):
@SlaunchaMan
SlaunchaMan / SetInit.swift
Created November 30, 2015 03:27
Initializing a Set without an array
extension Set {
init(items: Element...) {
self.init(items)
}
}
// From this: Set([object, object, object])
// To this: Set(items: object, object, object)
@SlaunchaMan
SlaunchaMan / CLKTextProvider+NNNCompoundTextProviding.h
Created August 24, 2015 04:06
Getting around Swift 2’s lack of compatibility with vararg methods for complication text providers.
@interface CLKTextProvider (NNNCompoundTextProviding)
+ (nonnull CLKTextProvider *)nnn_textProviderByJoiningProvider:(nonnull CLKTextProvider *)provider1 andProvider:(nonnull CLKTextProvider *)provider2 withString:(nullable NSString *)joinString;
@end
@SlaunchaMan
SlaunchaMan / Snap LICEcap to Watch Simulator.scpt
Last active October 7, 2015 19:06
Snap LICEcap to cover your simulator!
tell application "Simulator (Watch)" to activate
tell application "System Events"
set window_position to position of window 1 of process "Simulator (Watch)"
set window_size to size of window 1 of process "Simulator (Watch)"
end tell
set _x to item 1 of window_position
set _y to item 2 of window_position
set _width to item 1 of window_size