Это звучит как хороший пример для некоторых KVC (Key-Value Coding).
С помощью KVC вы можете создавать индексированные свойства и заставить механизм KVC создавать прокси-массив для индексированного свойства, что позволит вам работать с индексированным свойством, как если бы это был массив.
Ниже приведен краткий проверочный код, протестированный как на OS X, так и на iOS.
Интерфейс:
@property (strong) NSMutableArray *mainArray;
Реализация:
@synthesize mainArray = _mainArray;
- (id)init
{
self = [super init];
if (self) {
// For simplicity, use strings as the example
_mainArray = [NSMutableArray arrayWithObjects:
@"1st element",
@"2nd element",
@"3rd element",
@"4th element",
@"5th element",
@"6th element",
@"7th element",
@"8th element",
@"9th element",
@"10th element",
nil];
}
return self;
}
// KVC for a synthetic array, accessible as property @"secondaryArray"
- (NSUInteger) countOfSecondaryArray
{
return 5;
}
- (id) objectInSecondaryArrayAtIndex: (NSUInteger) index
{
// In practice you would need your mapping code here. For now
// we just map through a plain C array:
static NSUInteger mainToSecondaryMap[5] = {1,4,5,7,8};
return [self.mainArray objectAtIndex:mainToSecondaryMap[index]];
}
- (void) watchItWork
{
NSArray *secondaryArray = [self valueForKey:@"secondaryArray"];
// See how the sub array contains the elements from the main array:
NSLog(@"%@", secondaryArray);
// Now change the main array and watch the change reflect in the sub array:
[self.mainArray replaceObjectAtIndex:4 withObject:@"New String"];
NSLog(@"%@", secondaryArray);
}
Более подробная информация содержится в документах , в частности, в части по шаблону индексированного средства доступа.