Skip to content

Instantly share code, notes, and snippets.

View pilky's full-sized avatar

Martin Pilkington pilky

View GitHub Profile
@pilky
pilky / gist:4198723
Created December 3, 2012 22:29
NSOperation Global Background Queue
//NSOperationQueue+GlobalBackgroundQueue.h
@interface NSOperationQueue (GlobalBackgroundQueue)
+ (instancetype)backgroundQueue;
@end
//NSOperationQueue+GlobalBackgroundQueue.m
@pilky
pilky / gist:3865781
Created October 10, 2012 13:52
Test Managed Object Context
//HEADER
typedef NSArray *(^M3TestFetchRequestBlock)(NSFetchRequest *, NSError **);
@interface M3TestManagedObjectContext : NSManagedObjectContext
@property (copy) M3TestFetchRequestBlock fetchRequestBlock;
@end
@pilky
pilky / gist:3609846
Created September 3, 2012 14:51
NSLayoutConstraint code, done right
//Pass in arrays of views and access with number keys or add attributes for all
NSArray *views = @[ topView, middleView, lastView ];
[containerView setSubviews:views];
[containerView m3_addConstraints:@[
@"$1.top = 0",
@"$2.top = $1.bottom",
@"$3.top = $2.bottom",
@"$3.bottom = 0",
@"$all.(left, right) = (0, 10)", //Assign multiple attributes in one statement
@"$all.height >= 30"
@pilky
pilky / gist:2864689
Created June 3, 2012 19:17
Reactive Cocoa example with subscripting hack
Basically I can turn this:
[[[[loginResult
where:^(id x) {
return [x hasError];
}]
select:^(id x) {
return [x error];
}]
injectObjectWeakly:self]
@pilky
pilky / gist:2425052
Created April 20, 2012 00:37
This is a script that will calculate the number of subsets possible in a set of a given size
def fac(n, limit)
if n == 1
return 1
end
if n == limit
return limit
end
return n * fac(n - 1, limit)
end
- (void)submitDetails {
//If the user said not to send, don't
if (![[NSUserDefaults standardUserDefaults] boolForKey:TWSSendSystemProfile])
return;
//Generate our last sent check and see if it is different to the stored one
NSString *lastSent = [[NSDate date] descriptionWithCalendarFormat:@"%Y%m" timeZone:[NSTimeZone timeZoneForSecondsFromGMT:0] locale:nil];
if ([lastSent isEqualToString:[[NSUserDefaults standardUserDefaults] objectForKey:TWSLastSent]])
return;
- (void)sendDetails {
if ([self _shouldShowPrompt]) {
[permissionPromptController showWindow:self];
} else {
[submitter submitDetails];
}
}
- (BOOL)_shouldShowPrompt {
- (void)performLayout
{
[super performLayout];
myScrubView.frame = self.bounds;
BOOL reload = NO;
UIInterfaceOrientation orientation = [[UIApplication sharedApplication] statusBarOrientation];
if (orientation == UIInterfaceOrientationPortrait)
{
- (void)method1:(id)somethingElse {
NSString *identifier = [NSString stringWithFormat:@"%@.%@", [SomeClass someMethod], somethingElse];
MyObject *obj = [[MyObject alloc] initWithIdentifier:identifier];
}
- (void)method2:(id)somethingElse {
NSString *identifier = [NSString stringWithFormat:@"%@.%@", [SomeOtherClass someOtherMethod], somethingElse];
MyObject *obj = [[MyObject alloc] initWithIdentifier:identifier];
@pilky
pilky / ShowDerivedData.rb
Created November 5, 2011 14:24
A script for showing the derived data folder for an Xcode workspace. Intended to be invoked from a custom behaviour
#!/usr/bin/env ruby
#This script currently only works if you have your derived data in the default location
#First look for a project path as our workspace
xcodeWorkspacePath = ENV['XcodeProjectPath']
if xcodeWorkspacePath
workspaceName = /([\w\s]+).xcodeproj/.match(xcodeWorkspacePath)[1]
#If we don't have a project we have a workspace, check for that and exit with code 1 if that doesn't exist
else