Skip to content

Instantly share code, notes, and snippets.

@dtorres
Created June 1, 2013 07:53
Show Gist options
  • Save dtorres/5689620 to your computer and use it in GitHub Desktop.
Save dtorres/5689620 to your computer and use it in GitHub Desktop.
Disallow object subscripting in your project. NOTE: Do not include in Final Version of your project
//
// NSObject+NoSubscription.h
//
// Created by Diego Torres on 6/1/13.
// Copyright (c) 2013 Onda. All rights reserved.
//
#import <Foundation/Foundation.h>
@interface NSObject (NoSubscription)
@end
//
// NSObject+NoSubscription.m
//
// Created by Diego Torres on 6/1/13.
// Copyright (c) 2013 Onda. All rights reserved.
//
#import "NSObject+NoSubscription.h"
#import <objc/runtime.h>
@implementation NSObject (NoSubscription)
+ (void)load
{
[self swizzleInstanceSelector:@selector(setObject:atIndexedSubscript:) withNewSelector:@selector(ol_setObject:atIndexedSubscript:)];
[self swizzleInstanceSelector:@selector(setObject:forKeyedSubscript:) withNewSelector:@selector(ol_setObject:atIndexedSubscript:)];
[self swizzleInstanceSelector:@selector(objectAtIndexedSubscript:) withNewSelector:@selector(ol_objectAtIndexedSubscript:)];
[self swizzleInstanceSelector:@selector(objectForKeyedSubscript:) withNewSelector:@selector(ol_objectAtIndexedSubscript:)];
}
+ (void) swizzleInstanceSelector:(SEL)originalSelector
withNewSelector:(SEL)newSelector
{
Method originalMethod = class_getInstanceMethod(self, originalSelector);
Method newMethod = class_getInstanceMethod(self, newSelector);
BOOL methodAdded = class_addMethod([self class],
originalSelector,
method_getImplementation(newMethod),
method_getTypeEncoding(newMethod));
if (methodAdded) {
class_replaceMethod([self class],
newSelector,
method_getImplementation(originalMethod),
method_getTypeEncoding(originalMethod));
} else {
method_exchangeImplementations(originalMethod, newMethod);
}
}
- (void)ol_objectAtIndexedSubscript:(id)idx {
@throw [NSException exceptionWithName:@"NoSubscription" reason:@"Subscription is not allowed in this project" userInfo:nil];
}
- (void)ol_setObject:(id)obj atIndexedSubscript:(id)idx
{
@throw [NSException exceptionWithName:@"NoSubscription" reason:@"Subscription is not allowed in this project" userInfo:nil];
}
@end
@implementation NSArray (NoSubscription)
+ (void)load
{
[self swizzleInstanceSelector:@selector(setObject:atIndexedSubscript:) withNewSelector:@selector(ol_setObject:atIndexedSubscript:)];
[self swizzleInstanceSelector:@selector(setObject:forKeyedSubscript:) withNewSelector:@selector(ol_setObject:atIndexedSubscript:)];
[self swizzleInstanceSelector:@selector(objectAtIndexedSubscript:) withNewSelector:@selector(ol_objectAtIndexedSubscript:)];
[self swizzleInstanceSelector:@selector(objectForKeyedSubscript:) withNewSelector:@selector(ol_objectAtIndexedSubscript:)];
}
@end
@implementation NSDictionary (NoSubscription)
+ (void)load
{
[self swizzleInstanceSelector:@selector(setObject:atIndexedSubscript:) withNewSelector:@selector(ol_setObject:atIndexedSubscript:)];
[self swizzleInstanceSelector:@selector(setObject:forKeyedSubscript:) withNewSelector:@selector(ol_setObject:atIndexedSubscript:)];
[self swizzleInstanceSelector:@selector(objectAtIndexedSubscript:) withNewSelector:@selector(ol_objectAtIndexedSubscript:)];
[self swizzleInstanceSelector:@selector(objectForKeyedSubscript:) withNewSelector:@selector(ol_objectAtIndexedSubscript:)];
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment