Skip to content

Instantly share code, notes, and snippets.

@JohnSundell
JohnSundell / DictionaryMap.swift
Created June 5, 2017 10:21
A map method for Dictionary that lets you easily transform its keys and values into other types
extension Dictionary {
func map<K: Hashable, V>(_ transform: (Element) throws -> (key: K, value: V)) rethrows -> [K : V] {
var transformed = [K : V]()
for pair in self {
let transformedPair = try transform(pair)
transformed[transformedPair.key] = transformedPair.value
}
return transformed
@djg
djg / reading-list.md
Last active March 21, 2025 08:41
Fabian's Recommened Reading List
@lattner
lattner / TaskConcurrencyManifesto.md
Last active May 3, 2025 05:28
Swift Concurrency Manifesto
@lukagabric
lukagabric / VersionsComparison.playground
Last active November 21, 2017 08:14
Comparing version strings
import UIKit
import Foundation
func compareVersions(version1: String, version2: String) -> ComparisonResult {
let version1Components = version1.components(separatedBy: ".")
let version2Components = version2.components(separatedBy: ".")
let length = max(version1Components.count, version2Components.count)
for i in 0 ..< length {
@rahulmutt
rahulmutt / Main.hs
Last active October 1, 2020 04:44
Fast coproducts for Haskell & Eta
#!/usr/bin/env stack
{- stack
--resolver lts-6.27
--install-ghc
runghc
--package containers
-}
{-# LANGUAGE ScopedTypeVariables #-}
{-# LANGUAGE PolyKinds #-}
{-# LANGUAGE DataKinds #-}
import Foundation
final class Sample: NSObject {
@objc dynamic var name: String = ""
}
class MyObj: NSObject {
@objc dynamic var test: String = ""
}
extension NSObjectProtocol where Self: NSObject {
@chriseidhof
chriseidhof / scanner.swift
Last active February 6, 2019 14:10
Scanning Sequences
import Foundation
// Alternatives to `Scanner` (before: `NSScanner`).
// A scanner only needs a way to peek and to move to the next token.
protocol ScannerProtocol {
associatedtype Token: Equatable
var peek: Token? { get }
mutating func moveToNextToken()
@mikeash
mikeash / xcode.sh
Created September 15, 2017 17:07
Convince Xcode 9 not to smooth its source code font
#!/bin/bash
# Exit the script immediately on error
set -e
# We'll work in /tmp
cd /tmp
# Clone mach_override unless we already have it
if [ ! -d mach_override ]; then
@JohnSundell
JohnSundell / Podfile
Last active September 4, 2019 15:20
A Podfile that demonstrates how to use dependencies that use an older Swift version
target 'MyTarget' do
use_frameworks!
# Post installation script that enables the Swift 4.2 compiler's
# legacy 4.1 mode for 4.2-incompatible pods
post_install do |installer|
incompatiblePods = ['PodA', 'PodB']
installer.pods_project.targets.each do |target|
if incompatiblePods.include? target.name
@JohnSundell
JohnSundell / CrossPlatformImages.swift
Last active January 5, 2025 10:16
An easy way to make code that uses UIImage cross-platform between iOS/tvOS & macOS
// Either put this in a separate file that you only include in your macOS target
// or wrap the code in #if os(macOS) / #endif
import Cocoa
// Step 1: Typealias UIImage to NSImage
typealias UIImage = NSImage
// Step 2: You might want to add these APIs that UIImage has but NSImage doesn't.
extension NSImage {