Skip to content

Instantly share code, notes, and snippets.

@tapoton
tapoton / Resilient.swift
Created May 15, 2025 14:27
Resilient enum wrapper
public enum Resilient<T: RawRepresentable> {
case known(T)
case unknown(T.RawValue)
public var known: T? {
switch self {
case .known(let value): value
case .unknown: nil
}
}
@tapoton
tapoton / Counter.swift
Created May 14, 2025 17:43
Data race demo
import Foundation
import Synchronization
final class Counter: Sendable {
private let mutex = Mutex(0)
var count: Int {
get {
return mutex.withLock { count in
return count
@tapoton
tapoton / DeleteBackwardTextField.swift
Created March 10, 2019 09:15
textField(_:shouldDeleteBackwardsCharactersIn:)
protocol MyTextFieldDelegate: UITextFieldDelegate {
/// Asks the delegate if the character before the cursor or currently selected text should be deleted.
///
/// - Parameters:
/// - textField: The text field containing the text.
/// - range: Range of the text that is going to be deleted. If cursor is at the beginning, range's `location` and `length` are 0.
/// - Returns: `true` if the specified text range should be deleted; otherwise, `false` to keep the old text.
func textField(_ textField: MyTextField, shouldDeleteBackwardsCharactersIn range: NSRange) -> Bool
}
#include <iostream>
#include <string>
#include <cmath>
using namespace std;
class LongLong {
long left;
@tapoton
tapoton / Localized.swift
Last active May 5, 2017 09:43
Strongly typed localization for domain grouped strings.
import Foundation
protocol Domain {
associatedtype Parent = Domain
static var name: String { get }
static var fullName: String { get }
}
@tapoton
tapoton / CoreDataParsingHelper.m
Last active December 7, 2015 16:47
Fast CoreData parsing helper method (uses MagicalRecord 2.x)
/**
* returns the dictionary of updated or created managed objects where the key is object's entityIdentifierKey value
* requires identifier to be unique
* parsingBlock needs to implement the updating of each object from with assosiated dictionary
*/
+ (NSDictionary *)parseObjectsForClass:(Class)objectClass
fromArray:(NSArray *)array
entityIdentifierKey:(NSString *)entityIdentifierKey
dictIdentifierKey:(NSString *)dictIdentifierKey
deleteUnmatched:(BOOL)deleteUnmatched