Skip to content

Instantly share code, notes, and snippets.

View hollance's full-sized avatar

Matthijs Hollemans hollance

View GitHub Profile
//
// NSArray-Blocks.h
// Handy codebits
//
// If you want to keep block definitions terse, simple and dynamic, have no
// problems with the incompatible block pointer types and you don't mind
// compiler warnings about sending a message without matching signature,
// DO NOT IMPORT THIS FILE, seriously.
//
// Created by Sijawusz Pur Rahnama on 15/11/09.
@hollance
hollance / gist:1534156
Created December 29, 2011 13:42
Loading view from nib
@implementation UIView (MHExtensions)
+ (id)mh_viewFromNibNamed:(NSString *)nibName owner:(id)ownerOrNil
{
NSArray *nib = [[NSBundle mainBundle] loadNibNamed:nibName owner:ownerOrNil options:nil];
for (id object in nib)
{
if ([object isKindOfClass:[self class]])
return object;
}
@hollance
hollance / gist:2211705
Created March 27, 2012 02:01
Using GCD to perform a task in the background
dispatch_async(dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0), ^
{
// do stuff here
dispatch_async(dispatch_get_main_queue(), ^
{
// do stuff on main thread
});
});
@hollance
hollance / gist:2936287
Created June 15, 2012 12:42
Drawing inner glow with Core Graphics
- (CGImageRef)createMaskFromAlphaChannel:(UIImage *)image
{
size_t width = image.size.width;
size_t height = image.size.height;
NSMutableData *data = [NSMutableData dataWithLength:width*height];
CGContextRef context = CGBitmapContextCreate(
[data mutableBytes], width, height, 8, width, NULL, kCGImageAlphaOnly);
@hollance
hollance / Explanation.md
Last active September 25, 2017 03:35
Communicate between objects using channels

Communicate between objects using channels

When you have two objects A and B, say two view controllers, that you want to have talk to each other, you can choose from the following options:

  • NSNotificationCenter. This is anonymous one-to-many communication. Object A posts a notification to the NSNotificationCenter, which then distributes it to any other objects listening for that notification, including Object B. A and B do not have to know anything about each other, so this is a very loose coupling. Maybe a little too loose...

  • KVO (Key-Value Observing). One object observes the properties of another. This is a very tight coupling, because Object B is now peeking directly into Object A. The advantage of KVO is that Object A doesn't have to be aware of this at all, and therefore does not need to send out any notifications -- the KVO mechanism takes care of this behind the scenes.

  • Direct pointers. Object A has a pointer to Object B and directly sends it messages when something of interest h

@hollance
hollance / NSObject+MHOverride.h
Created July 18, 2012 21:01
Overriding methods without making a subclass
@interface NSObject (MHOverride)
/*
* Dynamically overrides the specified method on this particular instance.
*
* The block's parameters and return type must match those of the method you
* are overriding. However, the first parameter is always "id _self", which
* points to the object itself.
*
@implementation MHButton
{
BOOL observing; // whether we're listening to changes in state
BOOL didDrawHighlighted; // used to optimize drawing
BOOL didDrawEnabled; // used to optimize drawing
}
@synthesize appearance;
- (void)setAppearance:(id <MHButtonAppearance>)newAppearance
@hollance
hollance / ExampleViewController.m
Last active June 3, 2021 07:39
You cannot easily cancel Objective-C blocks that are running on a background queue. One possible solution is to let the block capture an object -- the so-called "cancel ticket" -- that determines whether it should stop or not. Whoever holds the ticket can call [ticket cancel] to stop the block. Of course, this does require the block to periodica…
// Shows how to use the cancel ticket to abort the async block.
#import "ExampleViewController.h"
#import "MHCancelTicket.h"
@implementation ExampleViewController
{
MHCancelTicket *_cancelTicket;
}
@hollance
hollance / YourMainDataModel.m
Created August 24, 2013 13:39
Saving an NSMutableArray to a plist file using NSKeyedArchiver and NSCoding.
// Gets the path to the app's Documents folder
- (NSString *)documentsDirectory
{
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
return paths[0];
}
// Gets the path to the data file
- (NSString *)dataFilePath
{
@hollance
hollance / MotionStreak.h
Created January 28, 2014 16:34
Motion streak code using SKShapeNode that didn't make it into the book iOS Games by Tutorials (raywenderlich.com).
#import <SpriteKit/SpriteKit.h>
@interface MotionStreak : SKShapeNode
- (void)addPosition:(CGPoint)position;
- (void)updateStreak;
@end