Created
June 1, 2013 07:53
-
-
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
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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 |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// | |
// 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