Skip to content

Instantly share code, notes, and snippets.

@quickfingers
Last active August 29, 2015 14:07
Show Gist options
  • Save quickfingers/f59b51ec8e1785b00bda to your computer and use it in GitHub Desktop.
Save quickfingers/f59b51ec8e1785b00bda to your computer and use it in GitHub Desktop.
iOS Code Snippets
// Avoiding compiler warnings. common example: deprecation notices or retain-cyle false positive
#pragma clang diagnostic push
#pragma clang diagnostic ignored "-Wunused-variable"
id object = object?:[NSNull null];
#pragma clang diagnostic pop
// Creating a nonretained object value
id nonCopyable Object = ...;
id <NSCopying> copyableValue = [NSValue valueWithNonretainedObject:nonCopyable];
// Adding an anonymous observer to a notification center
NSNotificationCenter *center = [NSNotificationCenter defaultCenter];
[center addObserverForName:nil
object:nil
queue:[NSOperationQueue mainQueue]
usingBlock:^(NSNotification *notification) {
NSLog(@"%@", notification.name);
}];
// Accessing thread-unsafe objects from a thread dictionary
NSMutableDictionary *threadDictionary = [[NSThread currentThread] threadDictionary];
NSDateFormatter *dateFormatter = threadDictionary[@"dateFormatter"];
if(!dateFormatter)
{
dateFormatter = [NSDateFormatter alloc]init];
dateFormatter.locale = [NSLocale curentLocale];
dateFormatter.dateStyle = NSDateFormatterLongStyle;
dateFormatter.timeStyle = NSDateFormatterShortStyle;
threadDictionary[@"dateFormatter] = dateFormatter;
}
return dateFormatter;
// Static singletons
+ (instancetype)sharedInstance {
static id _sharedInstance =nil;
static dispatch_one(&onceToken, ^ {
_sharedInstance = [[self alloc]init];
// further config
}
}
// Determining the tye of a property
const char *attributes = property_getAttributes(class_getProperty([self class], setl_getName(@selector(property))));
NSString *typeAttribute [[[NSString stringWithUTF8String:attributes] componentSeparatedByString:@","] firstObject];
const char *propertyType = [[typeAttribute substringFromIndex:1] UTF8String];
if(strcmp(propertyType, @encode(float))==0)
{
// float
}else if(strcmp(propertyType, @encode(int))==0 {
// int
}
// Hiding a Class
__attribute__((visibility("hidden"))
@interface HiddenClass: SuperClass
// ...
@end
// Hiding Method
-(BOOL)respondsToSelector:(SEL)selector{
if(selector == @(selector(methodToHide))
{
return NO;
}
return [[self class] instancesRespondToSelector:selector];
}
// Determining the current system memory usage
#import <mac/mach.h>
#import <sys/sysctl.h>
vm_statistics_data_t vmStats;
mach_msg_type_number_t infoCount = HOST_VM_INFO_COUNT;
kern_return_t status = host_statistics(mach_host_self(), HOST_VM_INFO, (host_info_t)&vmStats, &infoCount);
natural_t memoryUsage = 0; // in bytes
if(status == KERN_SUCCESS) {
memoryUsage = vmStats.wire_count * 1024.0;
}
// Determining if ARC is avaialable
#if __has_feature(objc_arc)
// ARC is available
#endif
// Intentionally crashing the current process
__builtin_trap();
// Dispatching work asynchronously to a background queue
dispatch_queue_t backgroundQueue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_BACKGROUND, 0);
dispatch_async(backgroundQueue, ^ {
// do your fucking work here
dispatch_async(dispatch_get_main_queue(), ^{
// return resuslt / access to main thread
}
});
// Finding application support directory
[[[NSFileManager defaultManager] URLsForDirectory:NSApplicationSupportDirectory inDomains:NSUserDomainMask]firstObject];
// Finding cache directory
[[[NSFileManager defaultManager] URLsForDirectory:NSCacheDirectory inDomains:NSUserDomainMask]firstObject];
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment