Skip to content

Instantly share code, notes, and snippets.

View alanjrogers's full-sized avatar
💭
I may be slow to respond.

Alan Rogers alanjrogers

💭
I may be slow to respond.
  • Melbourne, Australia
View GitHub Profile
@alanjrogers
alanjrogers / AttributeTest.m
Created September 21, 2012 01:42
__attribute(NSObject)__ test
#import <Foundation/Foundation.h>
@interface TestClass : NSObject
@property (nonatomic, strong) __attribute__((NSObject)) CFStringRef str;
@end
@implementation TestClass
@end
int main(int argc, char* argv[]) {
@alanjrogers
alanjrogers / CoreData.m
Created July 24, 2012 05:15
Shows how to merge a NSManagedObjectContextDidSaveNotification from a different PSC
/* When adding the persistent store to a PSC
pragmaOptions = [[NSDictionary alloc] initWithObjectsAndKeys:@"WAL", @"journal_mode", nil];
storeOptions = [[NSDictionary alloc] initWithObjectsAndKeys:pragmaOptions, NSSQLitePragmasOption, nil];
This will allow multiple readers, and at most writer to connect to the Sqlite DB. Default journalling only allows multiple readers OR 1 writer (ie. writing blocks all reading).
*/
@alanjrogers
alanjrogers / KVO.m
Created July 4, 2012 01:15
KVO best practices
static NSString *MYObservationContext = @"MYObservationContext";
[object addObserver:self forKeyPath:@"key.path" options:(NSKeyValueObservingOptionOld | NSKeyValueObservingOptionNew) context:(void*)&MYObservationContext];
- (void)observeValueForKeyPath:(NSString *)keyPath ofObject:(id)object change:(NSDictionary *)change context:(void *)context {
if (context == &MYObservationContext) {
id oldValue = [change objectForKey:NSKeyValueChangeOldKey];
id newValue = [change objectForKey:NSKeyValueChangeNewKey];
// Do something
}
@alanjrogers
alanjrogers / WebKitCrash.m
Created April 18, 2012 05:49
WebKit crash
// When the dom node is [object HTMLObjectElement]
// element is not a valid object ==> crash
- (void)mouseMoved:(NSEvent *)theEvent {
NSPoint mouseLocation = [self.webView convertPoint:[theEvent locationInWindow] fromView:nil];
mouseLocation.y = [self.webView frame].size.height - mouseLocation.y;
id element2 = [[self.webView windowScriptObject] evaluateWebScript:[NSString stringWithFormat:@"document.elementFromPoint(%f,%f);", mouseLocation.x, mouseLocation.y]];
id element = [[self.webView windowScriptObject] evaluateWebScript:[NSString stringWithFormat:@"\"result: \" + document.elementFromPoint(%f,%f)", mouseLocation.x, mouseLocation.y]];
@alanjrogers
alanjrogers / CoreDataThreadingQuestion.m
Created October 4, 2011 04:23
Question regarding private queue concurrency
__block NSManagedObject* object = nil;
[privateQueueContext performBlockAndWait:^{
object = [SomeClass methodThatExecutesFetchRequestReturningObjectUsingContext:privateQueueContext];
}];
// Is it safe to use object here?
NSLog(@"object: %@", object);
@alanjrogers
alanjrogers / EnumerateStringArray.m
Created September 13, 2011 05:09
Enumerate Objects using block with NSString* param
NSArray* stringArray = [NSArray arrayWithObjects:@"One", @"Two", @"Three", @"Four", nil];
[stringArray enumerateObjectsUsingBlock:^(NSString* obj, NSUInteger idx, BOOL *stop) {
NSLog(@"%@", obj);
}];
@alanjrogers
alanjrogers / UINavigationController+CustomTransitionAnimation.m
Created November 11, 2010 03:07
Allow left or right transition for UINavigationController
- (void)loadViewController:(UIViewController*)viewController withTransition:(AJRNavigationControllerTransition)transition
{
// This currently only works for a landscape only App.
// Needs to check orientation as nav controller view isn't rotated.
NSString* subtype = kCATransitionFromBottom;
switch (transition)
{
case AJRNavigationControllerTransition_FromLeft:
{
@alanjrogers
alanjrogers / UIImageView+CATransition.m
Created November 11, 2010 03:00
Sample code showing how to add a CATransition to changing the image on a UIImageView
UIImage* newImage = [UIImage imageNamed:context];
// set up an animation for the transition the content
CATransition *animation = [CATransition animation];
[animation setDuration:0.25];
[animation setType:kCATransitionPush];
[animation setSubtype:kCATransitionFromRight];
[animation setTimingFunction:[CAMediaTimingFunction functionWithName:kCAMediaTimingFunctionEaseInEaseOut]];
[[self.contentImageView layer] addAnimation:animation forKey:@"SwitchToView1"];
@alanjrogers
alanjrogers / CALayer+CGImage.m
Created November 10, 2010 06:31
Creating a CGImage from a CALayer tree.
CGSize imageSize = CGSizeMake(1000, 1000);
CGColorSpaceRef colorSpace = CGColorSpaceCreateWithName(kCGColorSpaceGenericRGB);
CGContextRef theContext = CGBitmapContextCreate(NULL, imageSize.width, imageSize.height, 8, 4*imageSize.width, colorSpace, kCGImageAlphaPremultipliedLast);
[layerView.layer renderInContext:theContext];
// Layer tree has backgroundFilters set on some of the CALayers
// When it's rendered in the Bitmap Context, none of these filters are applied.
@alanjrogers
alanjrogers / UIWebViewNoScroll.m
Created October 29, 2010 22:49
Disabling a UIWebView's scroll
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
if (webView.subviews.count > 0)
{
id aView = [webView.subviews objectAtIndex:0];
if ([aView respondsToSelector:@selector(setScrollEnabled:)])
{
((UIScrollView*)aView).scrollEnabled = NO;
}