Skip to content

Instantly share code, notes, and snippets.

View iwill's full-sized avatar
Growing Well 🌱🌱

Míng iwill

Growing Well 🌱🌱
View GitHub Profile
@iwill
iwill / TypeErasure.swift
Last active November 17, 2021 08:33
Code Snippet: Type Erasure for Protocol with AssociatedType, Self and static methods
// Type-Erasure
// - seealso: [AnyIterator](https://github.com/apple/swift/blob/2fe4254cb712fa101a220f95b6ade8f99f43dc74/stdlib/public/core/ExistentialCollection.swift.gyb#L45)
// MARK: remove `Equatable` if not needed
public protocol Protocol: Equatable {
// MARK: `Protocol` requirements
associatedtype AssociatedType
func methodOfProtocol() -> Self.AssociatedType
}
@iwill
iwill / ExCodable.swift
Last active August 17, 2021 01:27
Code Snippet: Adopte to ExCodable protocol
<#extension/struct/class#> <#Type#>: ExCodable {
static let <#keyMapping#>: [KeyMap<<#SelfType#>>] = [
KeyMap(\.<#property#>, to: <#"key"#>),
<#...#>
]
init(from decoder: Decoder) throws {
try decode<#Reference#>(from: decoder, with: Self.<#keyMapping#>)
}
func encode(to encoder: Encoder) throws {
try encode(to: encoder, with: Self.<#keyMapping#>)
@iwill
iwill / script-template.sh
Created December 26, 2020 07:56 — forked from m-radzikowski/script-template.sh
Minimal safe Bash script template - see the article with full description: https://betterdev.blog/minimal-safe-bash-script-template/
#!/usr/bin/env bash
set -Eeuo pipefail
trap cleanup SIGINT SIGTERM ERR EXIT
script_dir=$(cd "$(dirname "${BASH_SOURCE[0]}")" &>/dev/null && pwd -P)
usage() {
cat <<EOF
Usage: $(basename "${BASH_SOURCE[0]}") [-h] [-v] [-f] -p param_value arg1 [arg2...]
Base64 = {
// @param n Number, 6 bits, e.g. 0b111111
// @return String, a char, e.g. "/"
encode6Bits: function(n) {
n = Math.min(Math.max(0, n), 63);
if (n >= 0 && n <= 25) return String.fromCharCode("A".charCodeAt(0) + n); // ["A", "Z"]
else if (n >= 26 && n <= 51) return String.fromCharCode("a".charCodeAt(0) + n - 26); // ["a", "z"]
else if (n >= 52 && n <= 61) return String.fromCharCode("0".charCodeAt(0) + n - 52); // ["0", "9"]
else if (n == 62) return "+";
else /* if (n == 63) */ return "/";
Base64 = Base64 || {
// @param n Number, 6 bits, e.g. 0b111111
// @return String, a char, e.g. "/"
encode6Bits: function(n) {
n = Math.min(Math.max(0, n), 63);
if (n >= 0 && n <= 25) return String.fromCharCode("A".charCodeAt(0) + n); // ["A", "Z"]
else if (n >= 26 && n <= 51) return String.fromCharCode("a".charCodeAt(0) + n - 26); // ["a", "z"]
else if (n >= 52 && n <= 61) return String.fromCharCode("0".charCodeAt(0) + n - 52); // ["0", "9"]
else if (n == 62) return "+";
else /* if (n == 63) */ return "/";
@iwill
iwill / iflet.m
Created August 15, 2020 10:32 — forked from CraigSiemens/iflet.m
if-let and guard macros for Objective C
#import <Foundation/Foundation.h>
// VARIABLE must be a variable declaration (NSString *foo)
// VALUE is what you are checking is not nil
// WHERE is an additional BOOL condition
#define iflet(VARIABLE, VALUE) \
ifletwhere(VARIABLE, VALUE, YES)
#define ifletwhere(VARIABLE, VALUE, WHERE) \
@iwill
iwill / staticlibobjcabichecker.rb
Created December 29, 2018 02:26 — forked from dynamicdispatch/staticlibobjcabichecker.rb
Simple ruby script to check that Objective-C static libraries are built with the latest ObjC ABI
#!/usr/bin/env ruby
require 'optparse'
require 'pathname'
require 'open3'
# This tool checks an input path for all static libraries *.a files
# and makes sure they were built with the most modern ObjC ABI version.
# Parse command line options.
@iwill
iwill / POD_NAME.podspec
Created December 8, 2018 06:29 — forked from phatblat/POD_NAME.podspec
POC of a Pod which installs a run script into Xcode (CocoaPods 0.33 or less)
Pod::Spec.new do |s|
s.name = 'POD_NAME'
s.version = '1.0.0'
s.license = { :type => 'MIT', :file => 'LICENSE' }
s.summary = 'Example of pod which installs a run script into the Xcode project (first target)'
s.homepage = 'https://github.com/phatblat/POD_NAME'
s.authors = { 'Ben Chatelain' => '[email protected]' }
s.source = { :git => 'https://github.com/phatblat/POD_NAME.git', :tag => s.version.to_s }
s.ios.deployment_target = '6.0'
@iwill
iwill / cocoapods_prepare_commands_paths.sh
Created December 7, 2018 09:41 — forked from niklasberglund/cocoapods_prepare_commands_paths.sh
Example of how you can get project dir and file path in a podspec's prepare_command
# Get project directory path
current_pwd="$PWD"
project_dir=`cd "../../"; pwd`
cd "$current_pwd"
# Get .xcodeproj file path (yes I know it's not a file)
project_file=`find "$project_dir" -maxdepth 1 -name "*.xcodeproj" | tail -1`
@iwill
iwill / typeOf.js
Last active April 11, 2018 07:24
typeOf
/*!
* Javascript library - $class v0.2
* http://github.com/iwill/
*/
/**
* typeOf with Object.prototype.toString + instanceof
* #see https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/Object/toString#Description
*/
function typeOf(obj) {