Skip to content

Instantly share code, notes, and snippets.

View AliSoftware's full-sized avatar

Olivier Halligon AliSoftware

View GitHub Profile
@AliSoftware
AliSoftware / Mirror+DebugDescription.swift
Created January 24, 2019 21:46
Some convenience methods using Mirror to build a nice custom debugDescription easily
extension Mirror {
/// Use this to help you implement a custom debugDescription listing all properties of your instances
///
/// - Parameters:
/// - subject: The instance for which to return the description.
///
/// Example usage:
///
/// extension MyType: CustomDebugStringConvertible {
@AliSoftware
AliSoftware / SwiftCopying.swift
Last active August 18, 2021 19:41
TypeSafe copy()/mutableCopy() (NSCopying/NSMutableCopying) in Swift
import Foundation
//: Swift type-safe protocol versions of (Mutable)Copying
protocol SwiftCopying {
associatedtype NonMutableType = Self
func clone() -> NonMutableType
}
extension SwiftCopying where Self: NSCopying {
func clone() -> NonMutableType {
return self.copy() as! NonMutableType
@AliSoftware
AliSoftware / pod_stats.rb
Last active March 12, 2021 13:42
Tools to investigate the state of our internal pods at a8c
#!/usr/bin/env ruby
require 'json'
require 'yaml'
require 'open-uri'
# [Array<String>] List of iOS/macOS apps
app_dirs = ['WordPress-iOS', 'woocommerce-ios', 'simplenote-ios', 'simplenote-macos', 'autoproxxy']
specs_cache = File.join(__dir__, 'pod_stats.cache')
import Foundation
protocol TransformerType {
associatedtype BaseType
associatedtype TypeForCoding: Codable
static var encodeTransform: (BaseType) throws -> TypeForCoding { get }
static var decodeTransform: (TypeForCoding) throws -> BaseType { get }
}
@propertyWrapper
@AliSoftware
AliSoftware / Generics-Macros.h
Last active December 11, 2020 17:18
ObjCGenerics
// Allow to use generics even if not supported yet
#if __has_feature(objc_generics)
#define NSArrayOf(x) NSArray<x>
#define NSMutableArrayOf(x) NSMutableArray<x>
#define NSDictionaryOf(x,y) NSDictionary<x, y>
#define NSMutableDictionaryOf(x, y) NSMutableDictionary<x, y>
#define NSSetOf(x) NSSet<x>
#define NSMutableSetOf(x) NSMutableSet<x>
#else
#define NSArrayOf(x) NSArray
@AliSoftware
AliSoftware / RegEx.swift
Created July 31, 2019 13:46
Simple wrapper around NSRegularExpression to provide a swiftier API and, ability to have matches exposing Range instead of NSRange
// Simple wrapper around NSRegularExpression to provide a swiftier API and, ability to have matches exposing Range instead of NSRange
import Foundation
struct RegEx {
let regex: NSRegularExpression
init(pattern: String, options: NSRegularExpression.Options = []) throws {
self.regex = try NSRegularExpression(pattern: pattern, options: options)
}
func using<T: AnyObject>(object: T, execute: (T) throws -> Void) rethrows -> T {
try execute(object)
return object
}
import UIKit
// Then in some configureView() function of an UIViewController or whatnot…
let label1 = using(UILabel()) {
@AliSoftware
AliSoftware / CopyLabel.swift
Last active June 20, 2020 11:42
An UILabel subclass which allows you to show the "copy" MenuController item to copy its content to the pasteboard
import UIKit
class CopyLabel : UILabel {
// MARK: Setup
override init(frame: CGRect) {
super.init(frame: frame)
configureMenu()
}
enum Demo {
case simple
case oneValue(Int)
case twoValues(String, Double)
case threeValues(one: Int, two: Float, [Int])
}
//: # Direct exposition in the enum
//: ## Sourcery Template
// Xcode 11b1
@propertyDelegate
struct Clamped<Value: Comparable> {
private var storage: Value
private var clamp: (Value) -> Value
init(min: Value, max: Value, initialValue: Value) {
let clampingFunction = { ($0...$0).clamped(to: min...max).lowerBound }
self.storage = clampingFunction(initialValue)