Skip to content

Instantly share code, notes, and snippets.

View gcbrueckmann's full-sized avatar

Georg C. Brückmann gcbrueckmann

View GitHub Profile
@gcbrueckmann
gcbrueckmann / NSException+GCBSwiftErrorConversion.h
Created May 30, 2022 13:51
Converting Objective-C exceptions to Swift
@import Foundation;
NS_ASSUME_NONNULL_BEGIN
extern NSString *const GCBConvertedObjCExceptionErrorDomain;
extern NSString *const GCBConvertedObjCExceptionUserInfoExceptionKey;
typedef NS_CLOSED_ENUM(NSInteger, GCBConvertedObjCExceptionErrorCode)
{
GCBConvertedObjCExceptionErrorCodeCaughtException = 1
@gcbrueckmann
gcbrueckmann / unzip.swift
Created May 7, 2021 11:15
Fans of the zip(_:_:) function may find that the opposite operation, while easy to implement using reduce(into:), is arguably not as readable. The unzip(_:) function helps with that.
/// Creates a pair of sequences built out of an underlying sequence of pairs, i.e. the inverse of `zip(_:_:)`.
///
/// let wordsAndNumbers = [
/// ("one", 1),
/// ("two", 2),
/// ("three", 3),
/// ("four", 4)
/// ]
///
/// let unzipped = unzip(wordsAndNumbers)
{
"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",
@gcbrueckmann
gcbrueckmann / UITableView+TypeSafeDequeuing.swift
Last active March 16, 2018 14:14
A UITableView extension that allows you to dequeue reusable cells in a type safe manner.
import UIKit
extension UITableView {
// Ensures that a dequeued cell is of the expected type and fails with a meaningful message, if it is not.
func dequeueReusableCell<Cell>(ofType type: Cell.Type, withIdentifier identifier: String, for indexPath: IndexPath) -> Cell {
let cell = dequeueReusableCell(withIdentifier: identifier, for: indexPath)
guard let cellOfCorrectType = cell as? Cell else {
preconditionFailure("Dequeued cell (\(cell)) was not of expected type (\(type))")
@gcbrueckmann
gcbrueckmann / JSONValue.swift
Last active January 22, 2023 14:46
Decode arbitrary JSON values using Swift.Decodable
import Foundation
/// Wrapper allowing decoding of arbitrary JSON values,
/// i. e. values whose specific type is not known at compile time.
///
/// This is a workaround for Swift 4’s decoding system not allowing
/// something like `JSONDecoder().decode([String: Any], forKey: .foo)`,
/// because `Any` does not (and cannot) conform to `Decodable`.
///
/// Access the underlying value using the `anyValue` property.
@gcbrueckmann
gcbrueckmann / AccessCoordination.swift
Created September 17, 2016 15:24
Coordinate concurrent access to shared resources
/// Coordinates access to a critical resources, allowing an arbitrary number of concurrent read operations that are mutually exclusive with single concurrent write operations.
///
/// This approach is similar to `dispatch_async()` and `dispatch_barrier_async()`, except access blocks are asynchronous, i.e. the barrier is only released once the access block calls its completion handler.
final class AccessCoordinator {
private let queue = DispatchQueue(label: "AccessCoordinator")
private let lock = NSRecursiveLock()
enum AccessType {
@gcbrueckmann
gcbrueckmann / NSURL+ActualHomeDirectory.swift
Created August 6, 2016 13:13
Determine the actual home directory URL from a sandboxed app
extension NSURL {
/// A URL identifying the user's home directory, typically in `/Users`.
///
/// Calling `NSHomeDirectory()` from a sandboxed app will return an descendant of the app sandbox.
/// This property on the other hand will return the actual home directory outside the sandbox.
/// Note that access restrictions still apply, e.g. testing access to the returned URL will normally fail.
static var actualHomeDirectoryURL: NSURL? {
let pw = getpwuid(getuid())
return NSURL(fileURLWithFileSystemRepresentation: pw.memory.pw_dir, isDirectory: true, relativeToURL: nil)
@gcbrueckmann
gcbrueckmann / SerialQueue.swift
Created August 5, 2016 12:53
A Grand Central Dispatch queue wrapper that manages a serial queue and is deadlock-safe
/// A Grand Central Dispatch queue wrapper that manages a serial queue.
/// You can dispatch blocks to the receiver asynchronously as well as synchronously
/// without running risk of a deadlock. This is in contrast to `dispatch_sync()`.
final class SerialQueue {
init() {
dispatch_queue_set_specific(queue, SerialQueue.queueIdentityKey, queueIdentity, nil)
}
private let queue = dispatch_queue_create(nil, DISPATCH_QUEUE_SERIAL)
@gcbrueckmann
gcbrueckmann / MindfulNavigationController.swift
Created December 9, 2015 08:36
UINavigationController by default doesn't forward user interface orientation callbacks to its child view controllers. This is a workaround.
import UIKit
final class MindfulNavigationController: UINavigationController {
/// Forward user interface orientation callbacks to top view controller.
var respectsTopViewControllerOrientationConfiguration = true
override func supportedInterfaceOrientations() -> UIInterfaceOrientationMask {
guard respectsTopViewControllerOrientationConfiguration,
let topViewController = topViewController else
{