Skip to content

Instantly share code, notes, and snippets.

View smic's full-sized avatar

Stephan Michels smic

View GitHub Profile
//: [Previous](@previous)
import Foundation
struct AnyEquatable {
private let isEqualTo: (Any) -> Bool
let value: Any
init<A: Equatable>(_ value: A) {
self.value = value
@krzysztofzablocki
krzysztofzablocki / FindFinalClasses.swift
Last active August 22, 2021 12:12
Swift makes classes final if possible
#!/usr/bin/env bash
<% for type in types.classes { -%>
<%_ if type.attributes["final"] != nil || type.attributes["open"] != nil || types.based[type.name]?.isEmpty == false { continue } -%>
<%_ _%>git grep -lz 'class <%= type.name %>' | xargs -0 perl -i'' -pE "s/class <%= type.name %>(?=\s|:)/final class <%= type.name %>/g"
<% } %>
anonymous
anonymous / xcode.sh
Created November 9, 2017 17:20
#!/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 / 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 {
@frankrausch
frankrausch / String+Hyphenation.swift
Last active June 15, 2023 16:45
Returns a String with soft hyphens (U+00AD)
import Foundation
extension String {
func hyphenated(languageCode: String) -> String {
let locale = Locale(identifier: languageCode)
return self.hyphenated(locale: locale)
}
func hyphenated(locale: Locale) -> String {
//
// Copyright © 2017 Tony Arnold. All rights reserved.
enum CompoundValue<Element: Hashable> {
case distinct(Element)
case indistinct(Set<Element>)
case empty
var distinctValue: Element? {
switch self {
@lattner
lattner / TaskConcurrencyManifesto.md
Last active April 29, 2025 02:59
Swift Concurrency Manifesto
@chriseidhof
chriseidhof / sample.swift
Last active December 6, 2019 22:52
Observable References
import Foundation
// A lens is a getter and a setter combined
struct Lens<Whole, Part> {
let get: (Whole) -> Part
let set: (inout Whole, Part) -> ()
}
// We can create a lens from a key path
extension Lens {
@mayoff
mayoff / main.m
Created May 31, 2017 15:43
adding objc_boxable to CoreGraphics structs
@import Foundation;
@import CoreGraphics;
typedef struct __attribute__((objc_boxable)) CGPoint CGPoint;
typedef struct __attribute__((objc_boxable)) CGSize CGSize;
typedef struct __attribute__((objc_boxable)) CGRect CGRect;
typedef struct __attribute__((objc_boxable)) CGVector CGVector;
int main(int argc, const char * argv[]) {
@autoreleasepool {
@phausler
phausler / main.swift
Created May 19, 2017 02:05
Hashability via Codability
import Foundation
struct HashingSingleValueEncodingContainer : SingleValueEncodingContainer {
var owner: HashingEncoder
mutating func combineHash<T>(of element: T?) where T: Hashable {
if let elt = element {
owner.combine(elt, hash: elt.hashValue) { (other: Any) -> Bool in
if let otherValue = other as? T {
return elt == otherValue