Skip to content

Instantly share code, notes, and snippets.

View frankus's full-sized avatar

Frank Schmitt frankus

  • Bellingham, WA, USA
View GitHub Profile
@frankus
frankus / gist:7992755
Created December 16, 2013 19:23
Category on UIBarButtonItem for flexible space (add an `autorelease` call for non-ARC code).
@interface UIBarButtonItem (FlexibleSpace)
+ (instancetype)barButtonItemFlexibleSpace;
@end
@implementation UIBarButtonItem (FlexibleSpace)
+ (instancetype)barButtonItemFlexibleSpace {
return [[UIBarButtonItem alloc] initWithBarButtonSystemItem:UIBarButtonSystemItemFlexibleSpace target:nil action:nil];
@frankus
frankus / UIImageView+MapSnapshot.h
Created March 31, 2014 23:28
MKMapSnapshot category on UIImageView
//
// UIImageView+MapSnapshot.h
// Awning
//
// Created by Frank Schmitt on 3/31/14.
//
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@frankus
frankus / gist:e9660a376451f21e73f3
Created August 20, 2014 20:31
Pretty good (North America only) phone number UITextField
- (BOOL)textField:(UITextField *)textField shouldChangeCharactersInRange:(NSRange)range replacementString:(NSString *)string {
if (textField == self.myPhoneTextField) {
NSString *newText = [textField.text stringByReplacingCharactersInRange:range withString:string];
BOOL deleting = [newText length] < [textField.text length];
NSString *stripppedNumber = [newText stringByReplacingOccurrencesOfString:@"[^0-9]" withString:@"" options:NSRegularExpressionSearch range:NSMakeRange(0, [newText length])];
NSUInteger digits = [stripppedNumber length];
if (digits > 10)
stripppedNumber = [stripppedNumber substringToIndex:10];
@frankus
frankus / AffectedRows.m
Last active August 29, 2015 14:05
Switch between sorted arrays with intelligent animations
/**
Returns the index paths of the rows affected by switching between two
sorted (but not necessarily unique) arrays.
@param old The array to be switched from.
@param new The array to be switched to.
@param keyPath the key path to compare ("@self" to use raw value)
@return An array of three arrays, the first containing index paths
for modified rows, the second for deleted rows, and the third for
//
// ExtendedCollectionView.h
//
// Created by Frank Schmitt on 7/11/14.
//
@interface ExtendedCollectionView : UICollectionView
@property (nonatomic) UIEdgeInsets touchAreaInsets;
+ (NSValueTransformer *)URLArrayTransformer {
return [MTLValueTransformer reversibleTransformerWithForwardBlock:^NSArray *(NSArray *URLStrings) {
NSMutableArray *result = [NSMutableArray arrayWithCapacity:[URLStrings count]];
for (NSString *URLString in URLStrings) {
[result addObject:[NSURL URLWithString:URLString]];
}
return result;
} reverseBlock:^NSArray *(NSArray *URLs) {
NSMutableArray *result = [NSMutableArray arrayWithCapacity:[URLs count]];
for (NSURL *URL in URLs) {

Keybase proof

I hereby claim:

  • I am frankus on github.
  • I am frankus (https://keybase.io/frankus) on keybase.
  • I have a public key whose fingerprint is 2A7F B0A5 A84C 5A18 8750 94B6 242B C728 204F BB64

To claim this, I am signing this object:

@frankus
frankus / gist:5ce28924819a812a2035
Created August 12, 2015 23:16
One-liner to find filenames whose header comments no longer match
find . -iname "*.[mh]" -exec sed -n "2s|^// ||p" "{}" \; -exec basename "{}" \; | uniq -u
@frankus
frankus / gist:f13830ea658bed55ca41
Created August 13, 2015 00:14
Updates Xcode's default file-header comment to correct filename
find . -iname '*.[mh]' -print0 | while IFS= read -r -d '' file; do basename=`basename "$file"`; sed -i .bak "2s|// .*\$|// $basename|" "$file"; done
@frankus
frankus / Pure functions.md
Last active April 29, 2021 16:50
Extracting Pure Functions in Swift Without Polluting the Global Namespace

For testability purposes (and to make things easier to reason about), sometimes it's nice if methods in a class are pure functions, that is, they don't contain any explicit or implicit references to self, or really anything other than the arguments that are passed in.

In the painfully contrived example below, method is a method (because it references both a property and another method using self) and isEven is a pure function (because it doesn't).

class MyClass {
  let number: Int
  
 init(number: Int) {