Skip to content

Instantly share code, notes, and snippets.

@stephanecopin
stephanecopin / Publishers+CombineLatestMany.swift
Created February 13, 2020 00:58
An implemention of `CombineLatestMany` for Combine (Swift), which takes takes an array of `Publisher` and return a collection of values, based on their latest value.
import Combine
private protocol LockImplementation {
mutating func lock()
mutating func `try`() -> Bool
mutating func unlock()
}
private struct UnfairLock: LockImplementation {
private var unfairLock = os_unfair_lock_s()
@stephanecopin
stephanecopin / RFC2368EmailSupport.swift
Last active November 7, 2023 16:32
A small set of struct/extensions to easily handle `mailto:` links in Swift 5.1.
import Foundation
import MessageUI
struct EmailParameters {
/// Guaranteed to be non-empty
let toEmails: [String]
let ccEmails: [String]
let bccEmails: [String]
let subject: String?
let body: String?
@stephanecopin
stephanecopin / bitrise_import_additional_prov_profiles.rb
Last active October 4, 2019 20:47
The code for a Ruby Step on Bitrise allowing to import profiles from a zip archive from the Generic File Storage (must be the env var `ADDITIONAL_PROVISIONING_PROFILES_ARCHIVE_URL`), if hitting the 30 prov profiles limit.
require 'fileutils'
require 'tmpdir'
require 'English'
prov_profiles_install_path = '~/Library/MobileDevice/Provisioning Profiles'
prov_profiles_archive_url = ENV['ADDITIONAL_PROVISIONING_PROFILES_ARCHIVE_URL']
temporary_dir = File.join(Dir.tmpdir, "additional_prov_profiles")
@stephanecopin
stephanecopin / pod_install.sh
Last active December 5, 2022 06:46
Pod install script that automatically runs `pod install --repo-update` when appropriate
# Install Pods and use `--repo-update` if needed (might need to add sudo for the gem command below)
set -e
if [ -f "./Podfile" ] && [ ! -d "./Pods" ]; then
echo "Podfile found and Pods are not checked-in to git; installing Pods"
if [ -f "./Podfile.lock" ]; then
current_version=`pod --version | xargs echo -n`
podfile_version=`cat Podfile.lock | grep -E 'COCOAPODS: \d(\.\d)*' | sed -E 's/COCOAPODS: (.*)/\1/' | xargs echo -n`
if [ "$current_version" != podfile_version ]; then
echo "Cocoapods version $current_version doesn't match the Podfile.lock version $podfile_version, installing Podfile.lock version"
@stephanecopin
stephanecopin / EnumsDefaults.swifttemplate
Last active October 19, 2018 18:56
A Sourcery template for generating helper functions to emulate default parameters in enum case associated values
// swiftlint:disable all
// Copyright 2018 Stéphane Copin
//
// Permission is hereby granted, free of charge, to any person obtaining a copy of this software and associated documentation files (the "Software"), to deal in the Software without restriction, including without limitation the rights to use, copy, modify, merge, publish, distribute, sublicense, and/or sell copies of the Software, and to permit persons to whom the Software is furnished to do so, subject to the following conditions:
//
// The above copyright notice and this permission notice shall be included in all copies or substantial portions of the Software.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// The goal of this file is to allow easy uncurrying of member methods.
// For example, if you have:
// final class MyClass {
// func doSomething(_ arg1: Bool, _ arg2: String) -> Bool {
// // Something...
// return true
// }
// }
// And you want to be able to call `doSomething` as a regular function, you'd have to do `MyClass.doSomething(myClassInstance)(arg1, arg2)
// With this, you can do `uncurry(MyClass.doSomething)(myClassInstance, arg1, arg2)` which can be helpful and desirable.
@stephanecopin
stephanecopin / Version.swift
Last active March 29, 2023 13:53
A simple Semantic Version struct for Swift. Can be used to parse short app version and compare them easily.
struct Version {
let major: UInt
let minor: UInt
let patch: UInt?
init(major: UInt, minor: UInt = 0, patch: UInt? = nil) {
self.major = major
self.minor = minor
self.patch = patch
}
protocol EnumDecodable: RawRepresentable, Decodable {
static func defaultDecoderValue() throws -> Self
}
enum EnumDecodableError: Swift.Error {
case noValue
}
extension EnumDecodable {
@stephanecopin
stephanecopin / weak-target-for-timer.swift
Last active April 9, 2018 20:23
Extension to create Timer/CADisplayLink which don't strongly reference their target
private class WeakTarget: NSObject {
private(set) weak var target: AnyObject?
let selector: Selector
static let triggerSelector = #selector(WeakTarget.timerDidTrigger(parameter:))
init(_ target: AnyObject, selector: Selector) {
self.target = target
self.selector = selector
@stephanecopin
stephanecopin / LICENSE
Created April 20, 2016 03:04
Animate NSFetchedResultsController changes when setting the fetch request's predicate or sortDescriptors property
The MIT License (MIT)
Copyright (c) 2016 Fueled
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions: