Skip to content

Instantly share code, notes, and snippets.

@huandu
Created January 7, 2015 04:15
Show Gist options
  • Star 1 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save huandu/0519f6a04bae9378dc7a to your computer and use it in GitHub Desktop.
Save huandu/0519f6a04bae9378dc7a to your computer and use it in GitHub Desktop.
Proxy calls to all Obj-C objects
The MIT License (MIT)
Copyright (c) 2015 Huan Du
Permission is hereby granted, free of charge, to any person obtaining a copy
of this software and associated documentation files (the "Software"), to deal
in the Software without restriction, including without limitation the rights
to use, copy, modify, merge, publish, distribute, sublicense, and/or sell
copies of the Software, and to permit persons to whom the Software is
furnished to do so, subject to the following conditions:
The above copyright notice and this permission notice shall be included in all
copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL THE
AUTHORS OR COPYRIGHT HOLDERS BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER
LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM,
OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE
SOFTWARE.
// Copyright 2015 Huan Du. All rights reserved.
// Licensed under the MIT license that can be found in the LICENSE file.
#import <Foundation/Foundation.h>
#import "ProxyContainer.h"
int main(int argc, const char * argv[]) {
@autoreleasepool {
NSMutableArray *arr1 = [[NSMutableArray alloc] init];
NSMutableArray *arr2 = [[NSMutableArray alloc] init];
NSMutableArray *arr3 = [[NSMutableArray alloc] init];
[arr1 addObject:@"Hello"];
[arr1 addObject:@"world!"];
[arr2 addObject:@"Oh"];
id container = [[ProxyContainer alloc] initWithObjects:@[arr1, arr2, arr3]];
[container addObject:@"Roger"];
[container addObject:@"that"];
// arr1 has 4 objects: @"Hello", @"world!", @"Roger", @"that".
NSLog(@"%@", arr1);
// arr2 has 3 objects: @"Oh", @"Roger", @"that".
NSLog(@"%@", arr2);
// arr3 has 2 objects: @"Roger", @"that".
NSLog(@"%@", arr3);
}
return 0;
}
// Copyright 2015 Huan Du. All rights reserved.
// Licensed under the MIT license that can be found in the LICENSE file.
#import <Foundation/Foundation.h>
@interface ProxyContainer : NSProxy
- (instancetype)initWithObjects:(NSArray *)objects;
- (NSMethodSignature *)methodSignatureForSelector:(SEL)selector;
- (void)forwardInvocation:(NSInvocation *)invocation;
@end
// Copyright 2015 Huan Du. All rights reserved.
// Licensed under the MIT license that can be found in the LICENSE file.
#import "ProxyContainer.h"
@interface ProxyContainer ()
@property (nonatomic, strong, readonly) NSArray *managedObjects;
@end
@implementation ProxyContainer
- (instancetype)initWithObjects:(NSArray *)objects
{
_managedObjects = objects;
// NSProxy doesn't response init message.
return self;
}
- (NSMethodSignature *)methodSignatureForSelector:(SEL)selector
{
id obj = self.managedObjects.firstObject;
return [obj methodSignatureForSelector:selector];
}
- (void)forwardInvocation:(NSInvocation *)invocation
{
for (id obj in self.managedObjects) {
[invocation invokeWithTarget:obj];
}
}
@end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment