Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    text
    copied!<p>If you do want to bind to specific array indices, you could could create a wrapper object. Something like this. It lets you bind to <code>item0</code>, <code>item1</code> and so on. There is no range checking and it breaks if you change the size of the array, but you get the idea.</p> <p>Interface</p> <pre><code>@interface MyArrayBinder : NSObject { NSMutableArray *array; } - (id)initWithMutableArray:(NSMutableArray *)theArray; - (NSMutableArray *)array; @end </code></pre> <p>Implementation</p> <pre><code>#include &lt;objc/runtime.h&gt; static NSInteger _indexFromSelector(SEL sel) { return [[NSStringFromSelector(sel) stringByTrimmingCharactersInSet:[NSCharacterSet letterCharacterSet]] integerValue]; } static void _dynamicSetItem(MyArrayBinder *self, SEL sel, id obj) { [self.array replaceObjectAtIndex:_indexFromSelector(sel) withObject:obj]; } static id _dynamicItem(MyArrayBinder *self, SEL sel) { return [self.array objectAtIndex:_indexFromSelector(sel)]; } @implementation MyArrayBinder - (id)initWithMutableArray:(NSMutableArray *)theArray { self=[super init]; if (self) { array=theArray; for(NSUInteger i=0; i&lt;[array count]; i++) { class_addMethod([self class], NSSelectorFromString([NSString stringWithFormat:@"item%lu", i]), (IMP) _dynamicItem, "@@:"); class_addMethod([self class], NSSelectorFromString([NSString stringWithFormat:@"setItem%lu:", i]), (IMP) _dynamicSetItem, "v@:@"); } } return self; } - (NSMutableArray *)array { return array; } @end </code></pre>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload