This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@import Foundation; | |
#define ENABLE_LOGGING 1 | |
#if ENABLE_LOGGING | |
#define DHLog(__FORMAT__, ...) NSLog((__FORMAT__), ##__VA_ARGS__) | |
#else | |
#define DHLog(...) do { } while (0) | |
#endif |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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."]; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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: |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
- (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; | |
} | |
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
@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; | |
} | |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
--- | |
AccessModifierOffset: -2 | |
AlignAfterOpenBracket: true | |
AlignEscapedNewlinesLeft: false | |
AlignOperands: true | |
AlignTrailingComments: true | |
AllowAllParametersOfDeclarationOnNextLine: true | |
AllowShortBlocksOnASingleLine: false | |
AllowShortCaseLabelsOnASingleLine: true | |
AllowShortFunctionsOnASingleLine: All |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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; |