Skip to content

Instantly share code, notes, and snippets.

View beccadax's full-sized avatar

Becca Royal-Gordon beccadax

View GitHub Profile
beccadax / ANSession+discardUnusedResources.m
Created August 26, 2012 02:05
Suggested code to get rid of objects with no references except for their presence in a specific set
View ANSession+discardUnusedResources.m
// self.resources is an NSMutableSet of ANResource objects.
// I want to remove the objects that have no other references in the app.
// (This is intended to be called in response to an iOS memory warning.)
// Will this do the trick?
- (void)discardUnusedResources {
// We pass the objects through a weak reference; if they're not
// referenced anywhere else in the app, they won't survive the journey.
NSMutableSet * newResources = [NSMutableSet new];
NSUInteger oldResourceCount = self.resources.count;
beccadax / gist:3977323
Created October 29, 2012 23:32
Basic use of ANEntitySet and ANEntity
View gist:3977323
// Note that I have never run this code; it may have bugs or even be syntactically invalid.
- (NSAttributedString*)attributedTextForPost:(ANPost*)post {
NSMutableAttributedString * attributedText = [[NSMutableAttributedString alloc] initWithString:post.text];
for(ANEntity * entity in post.entities.all) {
[attributedText setAttributes:@{
NSForegroundColorAttributeName: [UIColor blueColor],
NSUnderlineStyleAttributeName: @(NSUnderlineStyleSingle),
@"entity": entity
beccadax / NSObject+performOptionalMethod.h
Created November 7, 2012 08:18
Category to make calling optional delegate methods simpler
View NSObject+performOptionalMethod.h
#import <Foundation/Foundation.h>
@protocol ADPerformOptionalMethod <NSObject>
- (instancetype)performOptionalMethod;
- (instancetype)performOptionalMethodOrReturnObject:(id)returnValue;
- (instancetype)performOptionalMethodOrReturnValue:(NSValue*)returnValue;
beccadax / gist:5036219
Created February 26, 2013 05:55
When a user notification fires while your Mac app is already in the foreground, this code plays the sound, but suppresses the rest of the notification. Put it in your user notification center delegate.
View gist:5036219
- (BOOL)userNotificationCenter:(NSUserNotificationCenter *)center shouldPresentNotification:(NSUserNotification *)note {
if(note.soundName) {
[[NSSound soundNamed:note.soundName] play];
return NO;
beccadax / gist:5098673
Created March 6, 2013 11:21
Stupid Macro Tricks™
View gist:5098673
// TsImageSizeConstraintType is an enum with these four values:
#define MAP \
ENTRY(TsEditionImageSizeConstraintNone) \
ENTRY(TsEditionImageSizeConstraintDimensions) \
ENTRY(TsEditionImageSizeConstraintPixelCount) \
NSString * TsStringForImageSizeConstraintType(TsImageSizeConstraintType type) {
switch(type) {
#define ENTRY(TYPE) case TYPE: return @#TYPE;
beccadax / NSObject+TsPropertyListRepresentation.h
Created March 6, 2013 11:52
Property list serialization protocol/category. This allows you to easily make an NSObject subclass generate a plist version of itself, or initialize itself from a plist. Simply conform to the TsPropertyListRepresentation protocol and call the +includeKeyInPropertyListRepresentation:... methods at an appropriate time, such as an +initialize metho…
View NSObject+TsPropertyListRepresentation.h
// NSObject+TsPropertyListRepresentation.h
// PressKit
// Created by Brent Royal-Gordon on 3/6/13.
// Copyright (c) 2013 Novelty Publishers. All rights reserved.
#import <Foundation/Foundation.h>
beccadax / NSSocketPort+initWithLocalhostTCPPort.h
Created April 12, 2013 03:17
Category on NSSocketPort to create ports bound only to `localhost`, and thus only accessible to other processes on the same Mac.
View NSSocketPort+initWithLocalhostTCPPort.h
// NSSocketPort+initWithLocalhostTCPPort.h
// Typesetter
// Created by Brent Royal-Gordon on 4/10/13.
// Copyright (c) 2013 Groundbreaking Software. All rights reserved.
#import <Foundation/Foundation.h>
beccadax / TsDistributedNotificationPortNameServer.h
Created April 12, 2013 03:28
Subclass of NSPortNameServer that uses distributed notifications (in a sandbox-friendly way) to implement a fast name server for socket ports. Decent demonstration of NSDistributedNotificationCenter, stupid run loop tricks, and SecTransform.
View TsDistributedNotificationPortNameServer.h
// TsDistributedNotificationPortNameServer.h
// Typesetter
// Created by Brent Royal-Gordon on 4/10/13.
// Copyright (c) 2013 Groundbreaking Software. All rights reserved.
#import <Foundation/Foundation.h>
beccadax / IndexSetEnumeration.m
Last active December 17, 2015 08:48
This gist includes a quick syntax for constructing NSIndexSets—[@0:9] and [@0:9 step:2]—and fast enumeration support for NSIndexSet (yielding NSNumbers). The result is that you can enumerate over ranges using for/in. The gist is CodeRunner ready.
View IndexSetEnumeration.m
#import <Foundation/Foundation.h>
@interface NSNumber (range)
- (NSIndexSet*):(NSUInteger)max;
- (NSIndexSet*):(NSUInteger)max step:(NSUInteger)step;
beccadax / ArchVulture.h
Last active December 18, 2015 12:59
Allows you to add a "vulture": a block that will be executed when the object it's attached to is deallocated.
View ArchVulture.h
#import <Foundation/Foundation.h>
@interface NSObject (addVulture)
// Do NOT use a block that captures this object.
// If you capture it strongly, you'll create a retain cycle.
// If you capture it weakly, you'll just get nil.
// If you capture it unsafely, you'll start calling methods on a mostly-dealloced object!
- (void)addVultureWithBlock:(dispatch_block_t)block;