Skip to content

Instantly share code, notes, and snippets.

View NorrinRadd's full-sized avatar

Brandon Trussell NorrinRadd

View GitHub Profile
#import <objc/runtime.h>
#import <objc/message.h>
#import <QuartzCore/QuartzCore.h>
#define PROPERTY(propName) NSStringFromSelector(@selector(propName))
// You should only use this in debug builds. It doesn't use private API, but I wouldn't ship it.
#ifdef DEBUG
static void PSPDFSwizzleMethod(Class c, SEL orig, SEL new) {
@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 / SlideKeyboard.m
Created February 11, 2014 19:03
slide keyboard in from the right. Looks for UITextEffectsWindow in jacky way.
[[NSNotificationCenter defaultCenter] addObserverForName:UIKeyboardWillShowNotification object:nil queue:[NSOperationQueue mainQueue] usingBlock:^(NSNotification *note)
{
NSLog(@"Windows: %@", [UIApplication sharedApplication].windows);
for (UIWindow *window in [UIApplication sharedApplication].windows)
{
if ([window.class.description rangeOfString:@"Text"].location != NSNotFound)
{
window.alpha = 0.0f;
@NorrinRadd
NorrinRadd / gist:f6073fc721224d69bc9f
Created June 12, 2014 14:06
Swift functions on all types
extension T! {
typealias ObjectiveCType = AnyObject
static func getObjectiveCType() -> Any.Type
func bridgeToObjectiveC() -> AnyObject
static func bridgeFromObjectiveC(x: AnyObject) -> T!?
static func isBridgedToObjectiveC() -> Bool
}
@NorrinRadd
NorrinRadd / gist:481c2815b7435c66a5c6
Created June 16, 2014 16:35
find class name in swift
func PrintableClassName<T>(variable : T) -> String
{
// Test for ObjC
if let v = variable as? NSObject
{
return v.classForCoder.description()
}
// If the object *can* be converted to ObjC type, do so
// However you lose any specifics as to type
@NorrinRadd
NorrinRadd / singleton
Last active August 29, 2015 14:03
singleton using init override
@interface blah : blah
-(id)init NS_UNAVAILABLE;
@end
@implementation
-(id)init{abort();}
-(instancetype)internalInit {
return [super init];
}
@NorrinRadd
NorrinRadd / gist:e4f2ce9694d3b28a751c
Last active August 29, 2015 14:03
description of mixing pointer and array syntax
[18:15:43] <Chris> Norrin: it's, like all C declarations, "declaration follows use".
[18:16:14] <Chris> So consider char (*x)[42]; You can then reason that *x is a char[42]. And (*x)[3] is a char.
[18:16:46] <Chris> Norrin: and in the case of char *x[42]; you can say that x[3] is a char * and *x[3] is a char.
so [] has precedence over *. hence why the () are required depending on intention.
[18:14:32] <Chris> Norrin: char *(*a)[42]; is a pointer to array of 42 char pointers.
public class MonoPInvokeCallbackAttribute : System.Attribute
{
private Type type;
public MonoPInvokeCallbackAttribute( Type t ) { type = t; }
}
/* Native binding */
extern "C" {
void _requestProducts(
char** sProductIdentifiers, uint32_t rp_len,
ManagedRequestCallback callback) {
NSMutableSet* productIdentifiers = [[NSMutableSet alloc] initWithCapacity:20];
for (int i=0; i<rp_len; i++) {
[productIdentifiers addObject:GetStringParam(sProductIdentifiers[i])];