Skip to content

Instantly share code, notes, and snippets.

View armcknight's full-sized avatar
🏔️

Andrew McKnight armcknight

🏔️
View GitHub Profile
@armcknight
armcknight / Rakefile-release_task.rb
Last active April 20, 2018 20:03
A rake task to release a cocoapod spec and record the version information to git.
desc 'Create git tags and push them to remote, push podspec to CocoaPods.'
task :release do
version = `vrsn --read --file #{version_file}`
sh "git tag #{version.strip}"
sh 'git push --tags'
sh 'pod trunk push'
end
@armcknight
armcknight / Rakefile-bump_task.rb
Last active April 20, 2018 20:03
Bumping versions in iOS apps and their repositories from the command line
desc 'Bump version number and commit the changes with message as the output from vrsn. Can supply argument to bump one of: major, minor, patch or build. E.g., `rake bump[major]` or `rake bump[build]`.'
task :bump,[:component] do |t, args|
require 'open3'
modified_file_count, stderr, status = Open3.capture3("git status --porcelain | egrep '^(M| M)' | wc -l")
if modified_file_count.to_i > 0 then
sh "git stash --all"
end
component = args[:component]
@armcknight
armcknight / returning-nonoptional-collections-and-throwing.swift
Created May 19, 2017 04:04
In model/business logic returning collections, returning nonoptional collection types simplifies consumer logic, and throwing errors clearly communicates failure modes.
func modelLayerFunction() throws -> [Any] { /* ... */ }
func controllerFunction() throws -> [Any] {
let models = try modelLayerFunction()
// ...
return models
}
do {
let results = try controllerFunction()
@armcknight
armcknight / returning-nil-collections.swift
Created May 19, 2017 04:04
In model/business logic returning collections, returning nil adds complexity to consumer logic.
func modelLayerFunction() -> [Any]? { /* ... */ }
func controllerFunction() -> [Any]? {
guard let models = modelLayerFunction() else {
// log error and/or show alert?
return nil
}
// ...
return models
}
@armcknight
armcknight / throwing-errors.swift
Last active May 19, 2017 03:39
Throwing an error in swift.
class AnObject {
func resultOfWork() throws -> Any {/* ... */}
}
func doSomeWork() throws -> Any {
let result = try anObjectInstance.resultOfWork()
// ...
return result
}
@armcknight
armcknight / nil-as-error.swift
Last active May 19, 2017 03:39
Using an optional return type and returning nil to signify a failure.
class AnObject {
func resultOfWork() -> Any? {/* ... */}
}
func doSomeWork() -> Any? {
guard let result = anObjectInstance.resultOfWork() else {
// log and/or show error alert here?
return nil
}
// ...
@armcknight
armcknight / bashorg_into_say_macos.sh
Last active May 19, 2017 03:25 — forked from triangletodd/bashorg_cowsay.sh
Retrieve a random quote from bash.org and pipe into macOS' say command. Probs not NSFW.
curl -s http://bash.org/?random1 \
| grep -oE "<p class=\"quote\">.*</p>.*</p>" \
| grep -oE "<p class=\"qt.*?</p>" \
| sed -e 's/<\/p>/\n/g' -e 's/<p class=\"qt\">//g' -e 's/<p class=\"qt\">//g' \
| perl -ne 'use HTML::Entities;print decode_entities($_),"\n"' \
| head -1 \
| say
@armcknight
armcknight / method-signature-with-block-typedef.h
Created April 3, 2017 03:33
Example of an Objective C method signature containing a block with an encapsulated type.
typedef void(^__nullable MyBlockType)(NSObject *__nullable a, NSObject *__nullable b, NSObject *__nullable c);
@interface MyClass
- (void)someMethodWithInput:(NSObject *__nonnull)input completion:(MyBlockType)completion;
@end
@armcknight
armcknight / method-signature-block-literal-antipattern.h
Last active April 3, 2017 03:31
Example of an Objective C method signature with a block parameter defined in literal syntax.
@interface MyClass
- (void)someMethodWithInput:(NSObject *__nonnull)input completion:(void(^__nullable)(NSObject *__nullable a, NSObject *__nullable b, NSObject *__nullable c))completion;
@end
@armcknight
armcknight / tag-icons.sh
Created April 2, 2017 03:08
Xcode app target icon tagging logic
XCODE_ICON_TAGGER_SCRIPT_MODE="$1"
function invoke_tagger() {
XCODE_ICON_TAGGER_TOOL_MODE="$1"
OPTIONAL_CUSTOM_ICON_TAG_TEXT="$2"
sh ${SRCROOT}/Vendor/XcodeIconTagger/tagIcons.sh \
$XCODE_ICON_TAGGER_TOOL_MODE \
${SRCROOT}/Trgnmtry/Assets.xcassets/AppIcon.appiconset \
$OPTIONAL_CUSTOM_ICON_TAG_TEXT