Skip to content

Instantly share code, notes, and snippets.

Avatar

Becca Royal-Gordon beccadax

View GitHub Profile
@beccadax
beccadax / ANSession+discardUnusedResources.m
Created Aug 26, 2012
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
beccadax / gist:3977323
Created Oct 29, 2012
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
beccadax / NSObject+performOptionalMethod.h
Created Nov 7, 2012
Category to make calling optional delegate methods simpler
View NSObject+performOptionalMethod.h
#import <Foundation/Foundation.h>
@protocol ADPerformOptionalMethod <NSObject>
@optional
- (instancetype)performOptionalMethod;
- (instancetype)performOptionalMethodOrReturnObject:(id)returnValue;
- (instancetype)performOptionalMethodOrReturnValue:(NSValue*)returnValue;
@beccadax
beccadax / gist:5036219
Created Feb 26, 2013
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
beccadax / gist:5098673
Created Mar 6, 2013
Stupid Macro Tricks™
View gist:5098673
// TsImageSizeConstraintType is an enum with these four values:
#define MAP \
ENTRY(TsEditionImageSizeConstraintNone) \
ENTRY(TsEditionImageSizeConstraintDimensions) \
ENTRY(TsEditionImageSizeConstraintPixelCount) \
ENTRY(TsEditionImageSizeConstraintByteCount)
NSString * TsStringForImageSizeConstraintType(TsImageSizeConstraintType type) {
switch(type) {
#define ENTRY(TYPE) case TYPE: return @#TYPE;
@beccadax
beccadax / NSObject+TsPropertyListRepresentation.h
Created Mar 6, 2013
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
beccadax / NSSocketPort+initWithLocalhostTCPPort.h
Created Apr 12, 2013
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
beccadax / TsDistributedNotificationPortNameServer.h
Created Apr 12, 2013
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
beccadax / IndexSetEnumeration.m
Last active Dec 17, 2015
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;
@end
@beccadax
beccadax / ArchVulture.h
Last active Dec 18, 2015
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;