Skip to content

Instantly share code, notes, and snippets.

@alemar11
alemar11 / FourColumns.swift
Created December 30, 2020 16:54 — forked from douglashill/FourColumns.swift
A sample UIKit app that sets up a four column layout with new iOS 14 API on UISplitViewController.
import UIKit
class FourColumnsContainerViewController: UIViewController {
let outerSplitViewController = UISplitViewController(style: .tripleColumn)
let innerSplitViewController = UISplitViewController(style: .doubleColumn)
let primary = makeContentViewController("App")
let secondary = makeContentViewController("Files")
let mainContent = makeContentViewController("File Content")
let inspector = makeContentViewController("Inspector")
@alemar11
alemar11 / Readme.markdown
Created November 14, 2020 15:24 — forked from calebd/Readme.markdown
Run Loop Source

CFRunLoopSource is cool. It lets you build behavior similar to the mechanisms that drive setNeedsLayout and setNeedsDisplay in UIKit.

I found myself in need of something like this a couple of times. It's great to know that no matter how many times I say I need to update something, I will get a single callback at the end of the run loop that gives me a chance to perform my work.

Here is a little Swift wrapper that makes the API easier to deal with.

//
// ProgrammerAssertions.swift
// Assertions
//
// Created by Mohamed Afifi on 12/20/15.
// Copyright © 2015 mohamede1945. All rights reserved.
//
import Foundation
@alemar11
alemar11 / HTTPStatusCode.swift
Created July 15, 2020 13:50 — forked from ollieatkinson/HTTPStatusCode.swift
HTTP status codes as a Swift enum.
/// This is a list of Hypertext Transfer Protocol (HTTP) response status codes.
/// It includes codes from IETF internet standards, other IETF RFCs, other specifications, and some additional commonly used codes.
/// The first digit of the status code specifies one of five classes of response; an HTTP client must recognise these five classes at a minimum.
enum HTTPStatusCode: Int, Error {
/// The response class representation of status codes, these get grouped by their first digit.
enum ResponseType {
/// - informational: This class of status code indicates a provisional response, consisting only of the Status-Line and optional headers, and is terminated by an empty line.
case informational
@alemar11
alemar11 / InteractiveTransitionCollectionViewDeselection.m
Created July 4, 2020 19:37 — forked from smileyborg/InteractiveTransitionCollectionViewDeselection.m
Animate table & collection view deselection alongside interactive transition (for iOS 11 and later)
// UICollectionView Objective-C example
- (void)viewWillAppear:(BOOL)animated {
[super viewWillAppear:animated];
NSIndexPath *selectedIndexPath = [[self.collectionView indexPathsForSelectedItems] firstObject];
if (selectedIndexPath != nil) {
id<UIViewControllerTransitionCoordinator> coordinator = self.transitionCoordinator;
if (coordinator != nil) {
[coordinator animateAlongsideTransition:^(id<UIViewControllerTransitionCoordinatorContext> context) {
@alemar11
alemar11 / TypingThrottler.swift
Created June 16, 2020 10:27
TypingThrottler
import Foundation
final class TypingThrottler {
typealias Handler = (String) -> Void
let interval: TimeInterval
let handler: Handler
private var workItem: DispatchWorkItem?
// handler will be called after the user has stopped typing for the given `interval` (seconds)
// Run any SwiftUI view as a Mac app.
import Cocoa
import SwiftUI
NSApplication.shared.run {
VStack {
Text("Hello, World")
.padding()
.background(Capsule().fill(Color.blue))
// a.swift
public dynamic func f() {
print("original")
}
// b.swift
import a
@_dynamicReplacement(for: f())
public func f_b() {
@alemar11
alemar11 / NSFormattingContextDynamic.m
Created March 15, 2020 10:20 — forked from michaelochs/NSFormattingContextDynamic.m
`NSFormattingContextDynamic` makes a formatter return string proxies that change based on where you but them inside a format string.
NSDate *date = [NSDate new];
NSDateFormatter *dateFormatter = [NSDateFormatter new];
dateFormatter.locale = [NSLocale localeWithLocaleIdentifier:@"nl_NL"];
dateFormatter.dateStyle = NSDateFormatterFullStyle;
dateFormatter.formattingContext = NSFormattingContextDynamic; // this is the important setting
NSString *dateString = [dateFormatter stringFromDate:date];
NSString *s1 = [NSString stringWithFormat:@"Foo %@", dateString]; // "Foo dinsdag 13 december 2016"
@alemar11
alemar11 / sample.swift
Created February 19, 2020 17:02 — forked from chriseidhof/sample.swift
View Inspection
import SwiftUI
struct SizeKey: PreferenceKey {
static func reduce(value: inout CGSize?, nextValue: () -> CGSize?) {
value = value ?? nextValue()
}
}
struct ContentView: View {
@State var width: CGFloat? = nil
var body: some View {