Skip to content

Instantly share code, notes, and snippets.

View zwaldowski's full-sized avatar

Zachary Waldowski zwaldowski

View GitHub Profile
@zwaldowski
zwaldowski / NSManagedObjectContext.swift
Created September 26, 2016 17:50
How Stella Got Her @NoEscape Back
// Swift 3 only; `@noescape` only applies to function definitions (not function types) in 2.x.
import CoreData
extension NSManagedObjectContext {
func performAndWaitOrThrow<Return>(_ body: () throws -> Return) rethrows -> Return {
func impl(execute work: () throws -> Return, recover: (Error) throws -> Void) rethrows -> Return {
var result: Return!
var error: Error?
@zwaldowski
zwaldowski / Extra Logging for My Great App.mobileconfig
Last active March 28, 2025 03:47
Apple Configuration Profile for Logging in iOS 10 and macOS Sierra
<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE plist PUBLIC "-//Apple//DTD PLIST 1.0//EN" "http://www.apple.com/DTDs/PropertyList-1.0.dtd">
<plist version="1.0">
<!-- iOS 10, macOS Sierra, and friends bring a new logging subsystem that's
supposed to scale from the kernel, up to frameworks, and up to apps. It defaults
to a more regimented, privacy-focused approach that large apps and complex
systems need.
It, along with Activity Tracing introduced in iOS 8 and macOS Yosemite and the
Console app in macOS Sierra, hope to help you graduate from caveman debugging to
@zwaldowski
zwaldowski / PersistentContainer.swift
Created September 2, 2016 06:48
NSPersistentContainer backport to iOS 7 with Swift 3
import CoreData
@objc(_PersistentStoreDescription) private protocol AnyPersistentStoreDescription: NSObjectProtocol, NSCopying {
var type: String { get set }
var configuration: String? { get set }
@objc(URL) var url: URL? { get set }
var options: [String : NSObject] { get }
func setOption(_ option: NSObject?, forKey key: String)
@zwaldowski
zwaldowski / Activity.swift
Last active November 3, 2024 17:37
os_activity_t for Swift 3
//
// Activity.swift
//
// Created by Zachary Waldowski on 8/21/16.
// Copyright © 2016 Zachary Waldowski. Licensed under MIT.
//
import os.activity
private final class LegacyActivityContext {
import Foundation
private extension NSPersonNameComponentsFormatter {
static let nameDefaultsByLocale: [String: AnyObject] = {
let bundle = NSBundle(forClass: NSPersonNameComponentsFormatter.self)
guard let url = bundle.URLForResource("NSPersonNameComponentsFormatterDefaults", withExtension: "plist"),
let dict = NSDictionary(contentsOfURL: url) as? [String: AnyObject] else { return [:] }
return dict["localeInfo"] as! [String: AnyObject]
import Darwin
extension Process {
// See also: https://github.com/apple/swift/blob/9cdbec13eee72feccfc5f8b987882a8c52e8107b/stdlib/private/SwiftPrivate/IO.swift#L84
public struct Stream: OutputStreamType {
private let fd: UnsafeMutablePointer<FILE>
private init(fd: UnsafeMutablePointer<FILE>) {
self.fd = fd
}
private func loadCFunction<Function>(named name: String, ofType _: Function.Type = Function.self) -> Function {
let sym = dlsym(UnsafeMutablePointer<Void>(bitPattern: -2), name) // RTLD_DEFAULT
return unsafeBitCast(sym, Function.self)
}
private let CC_SHA1_DIGEST_LENGTH = 20
private let CC_SHA1: @convention(c) (UnsafePointer<Void>, Int32, UnsafeMutablePointer<UInt8>) -> UnsafeMutablePointer<UInt8> = loadCFunction(named: "CC_SHA1")
extension String {
import QuartzCore
private final class PathContext {
let body: CGPath.Element -> Void
init(_ body: CGPath.Element -> Void) {
self.body = body
}
}
import UIKit
public protocol NibLoadable: class {
static var nibName: String { get }
static var nib: UINib { get }
}
extension NibLoadable {
@zwaldowski
zwaldowski / SigFigs.swift
Created April 18, 2016 15:11
Friendly rounding
import Foundation
let formatter = NSNumberFormatter()
formatter.currencyCode = "USD"
formatter.numberStyle = .CurrencyStyle
formatter.roundingMode = .RoundHalfUp
formatter.maximumSignificantDigits = 3
formatter.generatesDecimalNumbers = true
formatter.lenient = true