Skip to content

Instantly share code, notes, and snippets.

View hcrub's full-sized avatar

Neil Burchfield hcrub

View GitHub Profile
@hcrub
hcrub / Optional.swift
Last active May 3, 2019 21:28
Interface for determining and scoping optional Associated Types
/// Interface representing an optionally nil value type.
protocol OptionalType: ExpressibleByNilLiteral {
/// The boxed optional type.
associatedtype Wrapped
}
extension Optional: OptionalType {}
// Example
@hcrub
hcrub / ImageDataManager.swift
Last active November 22, 2023 21:56
Swift Image Manager Singleton
import UIKit
/// Data Manager error types.
///
/// - noData: No valid data was received.
/// - parsingFailure: The data parsing failed.
/// - endpointFailure: The endpoint failed.
/// - invalidInput: The input was invalid.
/// - anyError: Generic wrapped error.
enum DataError: Error {
@hcrub
hcrub / String+Capitalized.swift
Created April 8, 2017 22:47
Extension for checking if a String is capitalized in Swift 3.0+.
extension String {
func isCapitalized() -> Bool {
guard self.characters.count > 0 else {
return false
}
return (CharacterSet.uppercaseLetters as NSCharacterSet).characterIsMember(String(self.characters.first!).utf16[String.UTF16Index(0)]);
}
}
// Test
@hcrub
hcrub / NSRegularExpression+Split.swift
Last active August 25, 2023 15:10
Regular Expression "split" Implementation written for Swift 3.0+.
// NSRegularExpression+Split.swift
//
// Verbatim ObjC->Swift port originating from https://github.com/bendytree/Objective-C-RegEx-Categories
extension NSRegularExpression {
func split(_ str: String) -> [String] {
let range = NSRange(location: 0, length: str.characters.count)
//get locations of matches
@hcrub
hcrub / gist:7673617
Created November 27, 2013 10:30
Useful formatted git log output in bash
git log --graph --pretty=format:'%Cred%h%Creset -%C(white)%d%Creset %s'
// Output Example:
* fe14be7 - (HEAD, origin/development_v1.4.0, development_v1.4.0) Updated/Cleaned/Formatted Filter Controller
* dd5ac6e - Updated/Cleaned/Formatted Camera Controller
* 5fe469f - refs #3, CC Crash on Appending nil NSString
* 73b7655 - refs #5, Updated Sign-out route -> /venues/VENUE_ID/checkins/SESSION_ID
etc...
@hcrub
hcrub / PackTextures.sh
Created November 17, 2013 05:35
Pack Sprite Sheet Textures
#!/bin/bash
# ##########################################################
# Author: Neil Burchfield
# Purpose: Pack Textures Script
# Date: Nov 16, 2013
# ##########################################################
TP="/usr/local/bin/TexturePacker"
@hcrub
hcrub / LocalizeStrings.sh
Last active December 28, 2015 13:39
Shell Script (Xcode 5 tested) for localizing Localizable.strings into other languages.
#!/bin/bash
# ##########################################################
# Author: Neil Burchfield
# Purpose: Localization Script
# Date: Nov 16, 2013
# ##########################################################
# ##########################################################
# Vars
@hcrub
hcrub / HCClassAddIvar.m
Created November 7, 2013 17:42
Objective C Runtime's class_addIvar adding a new instance variable to a class
/**
* class_addIvar
* Adds a new instance variable to a class.
*
* BOOL class_addIvar(Class cls, const char *name, size_t size, uint8_t alignment, const char *types)
*
* Return Value
* YES if the instance variable was added successfully, otherwise NO.
**/
@hcrub
hcrub / ClassRespondsToSelector.m
Last active April 9, 2020 14:39
Objective C Runtime's class_respondsToSelector to detect if whether instances of a class respond to a particular selector.
/**
* class_respondsToSelector
* Returns a Boolean value that indicates whether instances of a class respond to a particular selector.
* Overall, class_respondsToSelector looks up the selector in the class's method table to see if it has an entry
*
* BOOL class_respondsToSelector(Class cls, SEL sel)
*
* Parameters
* cls
* The class you want to inspect.
@hcrub
hcrub / HCGetClassList.m
Created November 7, 2013 17:11
Objective C Runtime's objc_getClassList to obtain a list of registered class definitions
/**
* objc_getClassList
* Obtains the list of registered class definitions.
*
* int objc_getClassList(Class *buffer, int bufferLen)
*
* Parameters:
*
* buffer
* An array of Class values. On output, each Class value points to one class definition, up to either bufferLen or the total number of registered classes, whichever is less. You can pass NULL to obtain the total number of registered class definitions without actually retrieving any class definitions.