Skip to content

Instantly share code, notes, and snippets.

View douglashill's full-sized avatar

Douglas Hill douglashill

View GitHub Profile
@douglashill
douglashill / print-Core-Graphics.m
Last active August 29, 2015 14:03
Nicer than NSStringFromCGSize and friends
NSString *DHStringFromFloat(CGFloat value)
{
if (value == CGFLOAT_MAX) return @"max";
if (value == CGFLOAT_MIN) return @"min";
return [NSString stringWithFormat:@"%g", value];
}
NSString *DHStringFromPoint_bare(CGPoint point)
{
return [NSString stringWithFormat:@"%@, %@", DHStringFromFloat(point.x), DHStringFromFloat(point.y)];
@douglashill
douglashill / HTMLWriter.swift
Created July 9, 2014 16:57
Incomplete, but a promising start. I like either types.
// HTMLWriter.swift
// Douglas Hill, June 2014
// HTML syntax documentation: http://www.w3.org/TR/html-markup/syntax.html
enum HTMLContent {
case Element(HTMLElement)
case Text(String)
}
input = "NSHandlesContentAsCompoundValueBindingOption"
list = input.split /(?=[A-Z][a-z])/
if list.first.upcase == list.first
list.shift
end
puts list.map {|item| item.downcase}.join " "
@douglashill
douglashill / NSObject+DHRecursiveDescription.m
Last active August 29, 2015 14:06
NSObject recursive description category, for debugging trees (subviews, sublayers, child view controllers) or chains (superviews, next responders, superclasses).
@interface NSObject (DHRecursiveDescription)
- (NSString *)dh_recursiveDescriptionWithRecursion:(id (^)(id obj))recursion;
@end
@implementation NSObject (DHRecursiveDescription)
- (NSString *)dh_recursiveDescriptionWithRecursion:(id (^)(id obj))recursion
{
@implementation NSArray (DHShortDescription)
- (NSString *)dh_oneLineDescription
{
return [NSString stringWithFormat:@"[%@]", [self componentsJoinedByString:@", "]];
}
@end
#import <Foundation/Foundation.h>
void printInManyEncodings(u_int8_t hex)
{
NSData *const data = [NSData dataWithBytes:&hex length:sizeof(hex)];
NSDictionary *const encodings = @{
@"ASCII" : @(NSASCIIStringEncoding),
@"NEXTSTEP" : @(NSNEXTSTEPStringEncoding),
@"JapaneseEUC" : @(NSJapaneseEUCStringEncoding),
static void repeat(NSUInteger repeatCount, void (^blockToRepeat)(void))
{
if (blockToRepeat == nil) {
return;
}
while (repeatCount--) {
blockToRepeat();
}
}
@douglashill
douglashill / continued fractions.m
Created October 4, 2014 17:55
Continued fractions: convert real numbers to approximate fractions, such as 0.33 to 1/3 and π to 22/7.
@import Foundation;
#define ENABLE_LOGGING 1
#if ENABLE_LOGGING
#define DHLog(__FORMAT__, ...) NSLog((__FORMAT__), ##__VA_ARGS__)
#else
#define DHLog(...) do { } while (0)
#endif
@douglashill
douglashill / NSArray+DHAccumulate.m
Created October 5, 2014 11:34
Fold and reduce for NSArray. Mostly an academic exercise.
@import Foundation;
@implementation NSArray (DHAccumulate)
- (id)dh_objectByReducingWithOptions:(NSEnumerationOptions)options usingBlock:(id (^)(id object1, id object2))reductionBlock
{
if (options & NSEnumerationConcurrent) {
[NSException raise:NSInvalidArgumentException format:@"Bad things will happen if you try to reduce an array concurrently."];
}
@implementation NSColor (DHLuminance)
- (CGFloat)dh_luminance
{
return 0.3 * [self redComponent] + 0.6 * [self greenComponent] + 0.1 * [self blueComponent];
}
@end
// Example usage for an attributed string: