Skip to content

Instantly share code, notes, and snippets.

Olivier Halligon AliSoftware

Block or report user

Report or block AliSoftware

Hide content and notifications from this user.

Learn more about blocking users

Contact Support about this user’s behavior.

Learn more about reporting abuse

Report abuse
View GitHub Profile
View Clamped+PropertyWrapper.swift
// 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)
View Enum-Values-v1.swift
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
@AliSoftware
AliSoftware / Bindings.swift
Last active Jun 15, 2019
Re-implementation of @binding and @State (from SwiftUI) myself to better understand it
View Bindings.swift
// This is a re-implementation of the @Binding and @State property wrappers from SwiftUI
// The only purpose of this code is to implement those wrappers myself just to understand how they work internally and why they are needed
// Re-implementing them myself has helped me understand the whole thing better
//: # A Binding is just something that encapsulates getter+setter to a property
@propertyDelegate
struct XBinding<Value> {
var value: Value {
get { return getValue() }
@AliSoftware
AliSoftware / xcodeproj_check_files_in_proper_targets.rb
Last active May 2, 2019
This script walks thru all the files in a Xcode project's groups belong to the target they are supposed to, according to group names found while walking the groups hierarchy.
View xcodeproj_check_files_in_proper_targets.rb
#!/usr/bin/env ruby
# This script walks thru all the files in a Xcode project's groups belong to the target they are supposed to
# according to group names found while walking the groups hierarchy.
#
# This example only focuses on test files and groups/targets ending with "...Tests"
require 'xcodeproj'
def check(group, all_targets, walk_path, ctx_target_name = nil, ctx_expected_files_list = nil)
View Dangerfile-build_settings.rb
#######################################
# Check that some Build Settings (those defined by VERSION_SETTINGS below) are set on the right level (target/project)
#######################################
VERSION_SETTINGS = %w(GLOBAL_APP_VERSION GLOBAL_APP_BUILD_VERSION)
require 'xcodeproj'
project = Xcodeproj::Project.open('YOUR_PROJECT_NAME.xcodeproj')
target = project.targets.find("YOUR_TARGET_NAME").first
target.build_configurations.each do |bc|
## Don't allow some build settings at target level (but only at project level)
@AliSoftware
AliSoftware / Mirror+DebugDescription.swift
Created Jan 24, 2019
Some convenience methods using Mirror to build a nice custom debugDescription easily
View Mirror+DebugDescription.swift
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 / JSONDecoder+Nested.swift
Created Jan 24, 2019
Techniques to decode Decodable types that are nested under intermediate keys in a JSON
View JSONDecoder+Nested.swift
import Foundation
let json = """
{
"data": {
"items": [
{ "name": "Alice" },
{ "name": "Bob" },
{ "name": "Claudia" }
]
@AliSoftware
AliSoftware / CopyLabel.swift
Last active Jan 23, 2019
An UILabel subclass which allows you to show the "copy" MenuController item to copy its content to the pasteboard
View CopyLabel.swift
import UIKit
class CopyLabel : UILabel {
// MARK: Setup
override init(frame: CGRect) {
super.init(frame: frame)
configureMenu()
}
@AliSoftware
AliSoftware / lint_snippets_in_markdown.rb
Last active Feb 9, 2019
Runs SwiftLint on all code snippets found in a markdown file
View lint_snippets_in_markdown.rb
#!/usr/bin/env ruby
require 'tmpdir'
require 'open3'
# The first parameter is supposed to be the path to the markdown file
input_file = ARGV.first
config_file = ARGV[1] || '.swiftlint.yml'
config_param = File.exist?(config_file) ? " --config #{File.realpath(config_file)}" : ''
@AliSoftware
AliSoftware / FailableDecode.swift
Last active Jan 24, 2019
Decode an array of objects, allowing some of the items to fail without stopping the decoding of the rest
View FailableDecode.swift
import Foundation
//: ## Option 1: A Failable type for each item
//: In practice, it's very similar to the concept of the "Result" type that you see in most code bases (and that will be integrated in Swift 5), so if you already have a Result type, you might just want to use it instead, but if not, this is a super-simplification of it
enum Failable<T>: CustomStringConvertible {
case success(T)
case failure(Error)
var description: String {
You can’t perform that action at this time.