Skip to content

Instantly share code, notes, and snippets.

@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:
@douglashill
douglashill / UIScrollView+DHDebugImage.m
Created November 10, 2014 12:32
Useful for invisible scroll views
@implementation UIScrollView (DHDebugImage)
- (UIImage *)dh_debugImage
{
CGFloat scaleFactor = 0.1;
CGAffineTransform const transform = CGAffineTransformMakeScale(scaleFactor, scaleFactor);
CGRect const contentBounds = CGRectApplyAffineTransform((CGRect){.size = [self contentSize]}, transform);
static CGFloat const cornerRadius = 6;
@douglashill
douglashill / scrollViewDidScrollDebug.m
Last active August 29, 2015 14:10
Debug weird UIScrollView behaviour
- (void)scrollViewDidScroll:(UIScrollView *)scrollView
{
NSString *const stackSymbols = [[NSThread callStackSymbols] componentsJoinedByString:@""];
NSString *caller;
for (NSString *possibleCaller in @[@"UIAnimator", @"GestureRecognizer", @"smoothScroll", @"setContentOffset"]) {
if ([stackSymbols containsString:possibleCaller]) {
caller = possibleCaller;
break;
}
}
@douglashill
douglashill / NSFileManager+DHRemoveFile.m
Created January 7, 2015 16:46
NSFileManager DHRemoveFile category: remove an item, but don’t return an error if the item does not exist
@implementation NSFileManager (DHRemoveFile)
- (BOOL)dh_removeItemIfExistsAtURL:(NSURL *)URL error:(NSError **)out_error
{
NSError *error; // We need this in case out_error is NULL.
if ([self removeItemAtURL:URL error:&error]) {
return YES;
}
@douglashill
douglashill / non-square-problem.c
Created April 16, 2015 15:51
This demonstrates a problem discussed in Apple bug report 20428946: using LinearAlgebra’s la_solve with a non-square matrix results in an error instead of the expected output.
// xcrun -sdk macosx clang non-square-problem.c -framework Accelerate && ./a.out
// Douglas Hill, April 2015
// This demonstrates a problem discussed in Apple bug report 20428946.
// Using LinearAlgebra’s la_solve with a non-square matrix results in an error instead of the expected output.
#import <Accelerate/Accelerate.h>
static void solveUsingLinearAlgebra(float *Adata, float *bdata, int numObservations);
static void solveUsingLAPACK(float *Adata, float *bdata, int numObservations);
@douglashill
douglashill / _clang-format
Created May 9, 2015 15:29
My preferred configuration for ClangFormat with Objective-C code
---
AccessModifierOffset: -2
AlignAfterOpenBracket: true
AlignEscapedNewlinesLeft: false
AlignOperands: true
AlignTrailingComments: true
AllowAllParametersOfDeclarationOnNextLine: true
AllowShortBlocksOnASingleLine: false
AllowShortCaseLabelsOnASingleLine: true
AllowShortFunctionsOnASingleLine: All
@douglashill
douglashill / NSKeyedUnarchiver secure decoding.m
Last active August 29, 2015 14:26
How to enable secure decoding with NSKeyedUnarchiver
// It isn’t immediately obvious how to enable secure decoding with NSKeyedUnarchiver.
// NSKeyedUnarchiver has a read-write requiresSecureCoding property, but we normally use:
id decodedObject = [NSKeyedUnarchiver unarchiveObjectWithData:archive];
// so never see an instance to be able to set this property.
// However, it is quite simple. The critical part is that we use the
// normal decodeObject… methods, passing the key of the root object.
NSData *archive;
@douglashill
douglashill / UINavigationItem+DHDescription.m
Created September 13, 2015 17:40
A useful description method for UINavigationItem
@implementation UINavigationItem (DHDescription)
- (NSString *)description {
return [self dictionaryWithValuesForKeys:@[@"title", @"titleView", @"prompt", @"hidesBackButton", @"backBarButtonItem", @"leftItemsSupplementBackButton", @"leftBarButtonItems", @"rightBarButtonItems"]].description;
}
@end