Skip to content

Instantly share code, notes, and snippets.

View cbowns's full-sized avatar

Christopher Bowns cbowns

View GitHub Profile
@cbowns
cbowns / gist:6056535
Created July 22, 2013 18:57
A model object with a BOOL property backed by an NSNumber in the implementation
// An example of a model object with a boolean property
// that's backed by an NSNumber from a JSON response
@interface ModelObject : NSObject
@property (nonatomic, assign, getter = isEnabled) BOOL enabled;
@end
@interface ModelObject ()
@cbowns
cbowns / gist:5289226
Created April 2, 2013 01:25
Serializing an image's contents in the iOS Simulator
# In the debugger:
# (fill in <home> and <image> below with your user path and the image's ivar
p (BOOL)[(NSFileManager *)[NSFileManager defaultManager] createFileAtPath:@"<home>/Desktop/image.png" contents:(void *)UIImagePNGRepresentation(<image>) attributes:nil]
@cbowns
cbowns / UIApplication+NetworkActivity.h
Created September 19, 2012 00:29
UIApplication+NetworkActivity
//
// UIApplication+NetworkActivity.h
//
// Created by Christopher Bowns on 4/11/12.
// Original implementation: http://stackoverflow.com/a/6978817/774
// Copyright (c) 2012 Mechanical Pants Software
//
#import <UIKit/UIKit.h>
@cbowns
cbowns / gist:2992390
Created June 26, 2012 00:50
Helper function for moving UIView/CALayer centers when changing anchorPoints
- (CGPoint)center:(CGPoint)oldCenter movedFromAnchorPoint:(CGPoint)oldAnchorPoint toAnchorPoint:(CGPoint)newAnchorPoint withFrame:(CGRect)frame;
{
CGPoint anchorPointDiff = CGPointMake(newAnchorPoint.x - oldAnchorPoint.x, newAnchorPoint.y - oldAnchorPoint.y);
CGPoint newCenter = CGPointMake(oldCenter.x + (anchorPointDiff.x * frame.size.width),
oldCenter.y + (anchorPointDiff.y * frame.size.height));
return newCenter;
}
@cbowns
cbowns / gist:2963045
Created June 21, 2012 00:03
UIDevice System Version Preprocessor Macros
/*
System Versioning Preprocessor Macros via http://stackoverflow.com/questions/3339722/check-iphone-ios-version
Example:
if (UIDeviceSystemVersionLessThan(@"4.0")) {
// code for pre-4.0 devices.
}
if (UIDeviceSystemVersionGreaterThanOrEqualTo(@"3.1.1")) {
// Code for 3.1.1.
@cbowns
cbowns / IPInsetLabel.h
Created May 25, 2012 01:26
IPInsetLabel: a simple UILabel subclass that adds padding insets and auto-height-resizing
//
// IPInsetLabel.h
// Instapaper
//
// Created by Marco Arment on 7/23/11.
// Copyright 2011 Instapaper LLC, released to the public domain.
//
#import <UIKit/UIKit.h>
@cbowns
cbowns / gist:2706112
Created May 15, 2012 23:55
Nil-guarded dictionary key-value retrieval for JSON responses
// JSON is dangerous, take one of these!
+ (id)nullGuardedDict:(NSDictionary *)dict valueForKey:(NSString *)key;
{
id obj = nil;
obj = [dict valueForKey:key];
if (obj == [NSNull null]) {
obj = nil;
}
return obj;
@cbowns
cbowns / backup_growl.rb
Created May 15, 2012 16:37 — forked from bewebste/backup_growl.rb
Script to display Time Machine progress messages as Growl notifications
#!/usr/bin/ruby
#Just run this script in a terminal window, leaving the window open in the
#background so it can continue to process messages.
IO.popen("syslog -F \'$(Sender): $Message\' -w -k Sender com.apple.backupd") { |syslogIO|
while (inputString = syslogIO.gets) do
escapedString = inputString.gsub("'", "\\'")
`/usr/local/bin/growlnotify -a 'Time Machine' -t 'Time Machine' -m '#{escapedString}'`
end
}
@cbowns
cbowns / UIImage+MPImageNamedNilCheck.h
Created May 7, 2012 04:04
A nil-checking [UIImage imageNamed:] category. Uses thrown and caught exceptions to break into the debugger if you have exception breakpoints turned on. Use #define UIIMAGE_IMAGENAMED_NILCHECK 0 to turn off the throw-and-catch.
//
// UIImage+MPImageNamedNilCheck.h
//
// Created by Christopher Bowns on 5/6/12.
// Copyright (c) 2012 Mechanical Pants Software. All rights reserved.
//
#import <UIKit/UIKit.h>
@interface UIImage (MPImageNamedNilCheck)
@cbowns
cbowns / gist:2588474
Created May 3, 2012 19:24
subviewsAsAPI
// Please don’t do this.
// Need to change UISearchBar’s cancel button text?
// Well, there’s no API for that.
// (Sorry UIKit.)
UISearchBar *searchBar = [self.searchDisplayController searchBar];
for (UIView *view in [searchBar subviews]) { … <left as an exercise to the adventurous> }