Skip to content

Instantly share code, notes, and snippets.

View nsforge's full-sized avatar
🌿

Nick Forge nsforge

🌿
View GitHub Profile
@nsforge
nsforge / gist:73fa1d9c77808440a7a7
Last active December 17, 2015 05:13
Lack of warnings on [[self alloc] init] type errors
@interface MyClass : NSObject
@end
@implementation MyClass
+ (void)myMethod
{
NSArray *x = [[self alloc] init]; // This _doesn't_ throw a warning, despite +alloc and -init both being declared as instancetype
@nsforge
nsforge / gist:bba2fb28f9d745ff2c33
Created July 9, 2015 01:56
Related Object Lookup Pattern
@interface Person
// Derived from JSON
@property (readonly) NSString *personID;
@property (readonly) NSString *name;
@end
////////////////////////////////////////
@nsforge
nsforge / gist:71623cb0138a8210cc1f
Last active August 29, 2015 14:21
Private designated initializers in Objective-C
/*
My goal here is to have an immutable model class with an initialiser that is hidden unless you import a private header ("Person+Private.h"). This is useful in the context of providing a library or framework where consuming code only ever has read-only access to instances of this class, and they are never supposed to create instances themselves.
*/
//***********************************
// Person.h
//
@nsforge
nsforge / Warnings.xcconfig
Created February 26, 2015 04:39
Standard Xcode Warnings (for use in .xcconfig files)
// Apple LLVM - Warnings - All languages
GCC_WARN_CHECK_SWITCH_STATEMENTS = YES
GCC_WARN_ABOUT_DEPRECATED_FUNCTIONS = YES
CLANG_WARN_EMPTY_BODY = YES
GCC_WARN_SHADOW = YES
CLANG_WARN_CONSTANT_CONVERSION = YES
GCC_WARN_64_TO_32_BIT_CONVERSION = YES
CLANG_WARN_ENUM_CONVERSION = YES
CLANG_WARN_INT_CONVERSION = YES
GCC_WARN_INITIALIZER_NOT_FULLY_BRACKETED = YES
@nsforge
nsforge / gist:bf7c3d3ed2aba458e21f
Created November 18, 2014 11:25
One-liner to delete your oldest Time Machine backup
sudo tmutil delete "`tmutil listbackups | sed -n 1p`"
@nsforge
nsforge / gist:8949790
Created February 12, 2014 03:51
Demonstration of uint64 accuracy bug in NSJSONSerialization
NSString *jsonString = @"{\"myNumber\":1029742590268606522}";
NSData *jsonData = [jsonString dataUsingEncoding:NSUTF8StringEncoding];
NSDictionary *jsonDict = [NSJSONSerialization JSONObjectWithData:jsonData options:0 error:NULL];
NSLog(@"jsonDict: %@", jsonDict);
uint64_t myNumber = [jsonDict[@"myNumber"] unsignedLongLongValue];
NSLog(@"myNumber: %llu", myNumber);
/*
@nsforge
nsforge / gist:7743616
Created December 2, 2013 01:40
Example of why you shouldn't blindly throw away type information from the compiler when casting to a protocol
- (void)someCallbackWithAViewController:(UIViewController *)viewController
{
if ([viewController conformsToProtocol:@protocol(CrazyViewController)])
{
UIViewController <CrazyViewController> *crazyVC = (UIViewController <CrazyViewController> *)viewController;
if (crazyVC.presentedViewController == nil)
{
[crazyVC showCrazyModalView];
}
else
@nsforge
nsforge / gist:6861644
Created October 7, 2013 02:17
Things I've added to my .bash_profile to get C code compiling nicely with libraries installed via Homebrew
# Homebrew path config
export PATH="$PATH:/usr/local/bin:/usr/local/sbin"
export LIBRARY_PATH="$LIBRARY_PATH:/usr/local/lib"
export CPATH="$CPATH:/usr/local/include"
@nsforge
nsforge / gist:6861400
Created October 7, 2013 01:42
Getting RubyCocoa to work in ruby scripts running on OS X 10.9 Mavericks
# Change this:
...
#!/usr/bin/ruby
require 'osx/cocoa'
# My Script…
@nsforge
nsforge / gist:6805167
Last active December 24, 2015 13:29
KVC Helper Macros
// KVC Compile-time Helpers
//
// These macros allow simple compile-time checking of KVC keypaths, as long as the getter methods for properties
// match up with their property names (i.e. if the property 'available' has the getter method 'isAvailable', these
// macros will break)
#define Key(class, key) (0 ? ((class *)nil).key, (NSString *)nil : @#key)
#define InstanceKey(instance, key) (0 ? ((__typeof(instance))nil).key, (NSString *)nil : @#key)
#define SelfKey(key) (0 ? ((__typeof(self))nil).key, (NSString *)nil : @#key)
#define ProtocolKey(protocol, key) (0 ? ((id <protocol>)nil).key, (NSString *)nil : @#key)