Skip to content

Instantly share code, notes, and snippets.

@Air-Craft
Air-Craft / gcd_one_off_timer.m
Created January 30, 2015 20:27
One-off timer with GCD on high priority thread #concurrency #GCD #objective-c #advanced
dispatch_source_t _timer;
_timer = dispatch_source_create(DISPATCH_SOURCE_TYPE_TIMER, 0, 0, dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_HIGH, 0));
dispatch_source_set_timer(_timer,
dispatch_walltime(NULL, <#timeout#> * NSEC_PER_SEC),
1 * NSEC_PER_SEC, // this doenst really matter
<# 0.01 (tolerance) #> * NSEC_PER_SEC);
dispatch_source_set_event_handler(_timer, ^{
dispatch_source_cancel(_timer); // cancel the repeat
@Air-Craft
Air-Craft / objc_filtered_class_names.m
Created January 26, 2015 15:07
Get a list of all available Objective-C class names which match a predicate #objective-c #advanced #runtime #dynamic
+ (NSArray *)arrayOfClassNamesFilteredByPredicate:(NSPredicate *)predicate
{
int numClasses;
Class *classes = NULL;
classes = NULL;
numClasses = objc_getClassList(NULL, 0);
if (numClasses < 1 ) return nil;
@Air-Craft
Air-Craft / objc_filtered_class_objects.m
Created January 26, 2015 15:06
Get a list of all available Objective-C Class objects whose names match a predicate #objective-c #advanced #runtime #dynamic
+ (NSArray *)arrayOfClassObjectsFilteredByPredicate:(NSPredicate *)predicate
{
int numClasses;
Class *classes = NULL;
classes = NULL;
numClasses = objc_getClassList(NULL, 0);
if (numClasses < 1 ) return nil;
@Air-Craft
Air-Craft / objc_get_subclasses.m
Created January 26, 2015 15:05
Get a list of subclasses of an Objective-C class #advanced #objective-c #runtime #dynamic
+ (NSArray *)subclassesOfClass:(Class)parentClass
{
int numClasses = objc_getClassList(NULL, 0);
Class *classes = NULL;
classes = (__unsafe_unretained Class *)malloc(sizeof(Class) * numClasses);
numClasses = objc_getClassList(classes, numClasses);
NSMutableArray *result = [NSMutableArray array];
for (NSInteger i = 0; i < numClasses; i++)
@Air-Craft
Air-Craft / nsdate_to_age.m
Created December 19, 2014 12:01
Calculate person’s age from NSDate #objective-c #datetime #basic
NSDate *birthday = value;
NSDate *now = [NSDate date];
NSDateComponents *ageComponents = [[NSCalendar currentCalendar]
components:NSYearCalendarUnit
fromDate:birthday
toDate:now
options:0];
NSInteger age = [ageComponents year];
@Air-Craft
Air-Craft / sanitise_filter_strings.m
Created December 4, 2014 22:05
Sanitise string filtering out bad characters #strings #intermediate #objective-c
// Native...
NSCharacterSet* illegalFileNameCharacters = [NSCharacterSet characterSetWithCharactersInString:@"/\\?%*|\"<>"];
NSString *sanitized = [[fileName componentsSeparatedByCharactersInSet:illegalFileNameCharacters] componentsJoinedByString:@""];
// With Overline (negative matching)
NSString *sanitized = [fileName stringByReplacingOccurrencesOfRegExpPattern:@"[^a-zA-Z0-9_\\+\\-]+" withString:@"_"
@Air-Craft
Air-Craft / class_property_list.m
Created December 4, 2014 21:53
Get list of properties as an array of NSStrings #objective-c #intermediate #runtime
+ (NSArray *)classPropertyList
{
// Get array of all property names
unsigned int count;
objc_property_t *props = class_copyPropertyList(self, &count);
NSMutableArray *propNames = [NSMutableArray array];
for (int i=0; i<count; i++)
{
NSString *p = [NSString stringWithUTF8String:property_getName(props[i])];
[propNames addObject:p];
@Air-Craft
Air-Craft / objc_category_swizzle_inject.m
Last active August 29, 2015 14:06
Objective-C Category method swizzling and injection #runtime #advanced #objective-c #swizzle
// FOR CATEGORIES (From same class into same class)
// `swizzle()` allows replacing methods in a Category while still leaving access
// to the original akin to `super` for subclasses
// INJECT/REPLACE (loses original)
void (^inject)(Class, SEL, SEL) = ^(Class c, SEL orig, SEL new){
Method newMethod = class_getInstanceMethod(c, new);
class_replaceMethod(c, orig, method_getImplementation(newMethod), method_getTypeEncoding(newMethod));
};
@Air-Craft
Air-Craft / coregraphics_image_masking.m
Created September 16, 2014 17:54
CoreGraphic Image Masking #CoreGraphics #image-manipulation #intermediate
+ (UIImage *)imageOfRibbonBGWithFrame:(CGRect)frame
{
CGImageRef bgSolid = [super imageOfRibbonBGWithFrame:frame].CGImage;
CGImageRef holes = [super imageOfRibbonDotsWithFrame:frame].CGImage;
CGImageRef mask = CGImageMaskCreate(CGImageGetWidth(holes),
CGImageGetHeight(holes),
CGImageGetBitsPerComponent(holes),
CGImageGetBitsPerPixel(holes),
CGImageGetBytesPerRow(holes),
@Air-Craft
Air-Craft / ios_version_detection.m
Created September 16, 2014 14:57
Detect iOS version (iOS8 and old version) #ios #versioning #basic
// iOS >=8
NSOperatingSystemVersion ios8_0_0 = (NSOperatingSystemVersion){8, 0, 0};
if ([[NSProcessInfo processInfo] isOperatingSystemAtLeastVersion:ios8_0_0]) {
// iOS 8 logic
} else {
// iOS 7 and below logic
}
// iOS <=7