Skip to content

Instantly share code, notes, and snippets.

View douglashill's full-sized avatar

Douglas Hill douglashill

View GitHub Profile
@douglashill
douglashill / anchor-conversion.swift
Created May 4, 2016 09:44
Turn layout constraints created with anchors into the old API so it works before iOS 9
// TODO: greater than, less than, multipliers, constants
import Foundation
let input = "[container.trailingAnchor constraintEqualToAnchor:childView.trailingAnchor]"
func attribute(fromAnchor anchor: String) -> String {
if let range = anchor.rangeOfString("Anchor") {
var s = anchor.substringToIndex(range.startIndex)
let firstChar = s.removeAtIndex(s.startIndex)
@douglashill
douglashill / filename-in-preamble.py
Last active April 28, 2016 02:35
Make source code files in a directory all begin with consistent preamble
# coding: utf-8
import os
import sys
# Make source code files in a directory all begin with consistent preamble, which is:
# //
# // <filename>
#
# This code is very rough and could be better in lots of ways.
#!/usr/bin/python
#coding: utf-8
import json
import subprocess
def delete_all_simulators():
devices_json = subprocess.check_output(["xcrun", "simctl", "list", "--json", "devices"])
devices_by_runtime = json.loads(devices_json)["devices"]
for runtime, devices in devices_by_runtime.iteritems():
@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
// This is more hacky than it should be!
- (UIModalPresentationStyle)pspdf_currentPresentationStyle {
UIViewController *vc = self.presentedViewController.presentingViewController;
if ([self respondsToSelector:@selector(adaptivePresentationStyleForTraitCollection:)]) {
UIModalPresentationStyle const style = [self adaptivePresentationStyleForTraitCollection:vc.traitCollection];
return (style == UIModalPresentationNone) ? self.presentationStyle : style;
}
// Before iOS 8.3 we have to fall back on assuming adaptivity is only possible for horizontally compact environments.
@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 / covariance.m
Last active November 25, 2016 14:53
A quick look at variance with Objective-C generics. `NSSet` is covariant on its `ObjectType`, while `NSHashTable` is invariant. Contravariance is also supported.
@import Foundation;
@interface ContravariantCollection<__contravariant ObjectType> : NSObject
@end
@implementation ContravariantCollection
@end
void stringSetThing(NSSet<NSString *> *set) {
}
@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 / 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 / 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;
}