Skip to content

Instantly share code, notes, and snippets.

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 Sep 21, 2012
__attribute(NSObject)__ test
View AttributeTest.m
#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 Jul 24, 2012
Shows how to merge a NSManagedObjectContextDidSaveNotification from a different PSC
View CoreData.m
/* 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 Jul 4, 2012
KVO best practices
View KVO.m
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
}
View WebKitCrash.m
// 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 Oct 4, 2011
Question regarding private queue concurrency
View CoreDataThreadingQuestion.m
__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 Sep 13, 2011
Enumerate Objects using block with NSString* param
View EnumerateStringArray.m
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 Nov 11, 2010
Allow left or right transition for UINavigationController
View UINavigationController+CustomTransitionAnimation.m
- (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 Nov 11, 2010
Sample code showing how to add a CATransition to changing the image on a UIImageView
View UIImageView+CATransition.m
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 Nov 10, 2010
Creating a CGImage from a CALayer tree.
View CALayer+CGImage.m
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 Oct 29, 2010
Disabling a UIWebView's scroll
View UIWebViewNoScroll.m
- (void)webViewDidFinishLoad:(UIWebView *)webView
{
if (webView.subviews.count > 0)
{
id aView = [webView.subviews objectAtIndex:0];
if ([aView respondsToSelector:@selector(setScrollEnabled:)])
{
((UIScrollView*)aView).scrollEnabled = NO;
}