Skip to content

Instantly share code, notes, and snippets.

View NorrinRadd's full-sized avatar

Brandon Trussell NorrinRadd

View GitHub Profile
@NorrinRadd
NorrinRadd / TestClass.h
Created May 28, 2013 17:43
Before calling an optional protocol method, it is good to check if the delegate has implemented the methods. This is usually done using the following pattern: if (_delegate && [_delegate.respondsToSelector:@selector(method1)]) { // Do your thing } Turns out that some days ago, I faced a better way to do this kind of check by creating a struct an…
// TestClass.h
#import <Foundation/Foundation.h>
@protocol TestProtocol <NSObject>
@optional
- (void) testProtocolMethod;
@end
@interface TestClass: NSObject
{
// Struct that will hold the methods that the delegate implements
@NorrinRadd
NorrinRadd / gist:5664705
Created May 28, 2013 17:59
added to methods of a parent class so that subclasses are warned if they do not call super
#if __has_attribute(objc_requires_super)
#define NS_REQUIRES_SUPER __attribute((objc_requires_super))
#endif
@NorrinRadd
NorrinRadd / gist:3a461fe7bc714757b4b6
Created July 14, 2014 20:56
crazy interview questions
Q: What’s wrong with this code?
NSLock* arrayLock = GetArrayLock();
NSMutableArray* myArray = GetSharedArray();
id anObject;
[arrayLock lock];
anObject = [myArray objectAtIndex:0];
[arrayLock unlock];