Я создал изменяемый массив, называемый числами, и он содержит 20 различных чисел в случайном порядке.
как я видел после отладки, данные сохраняются в памяти и автоматически удаляются.
после генерации массива
я должен сохранить данные в массиве для последующего использования, и я могу найти способ сделать это, мне нужно иметь возможность читать числа в массиве, как это можно сделать?
может быть глобальный массив NSMutable?
код:
код, который я использовал для генерации массива и шифрования:
/*
Use scrambleArray to scramble any NSMutableArray into random order.
This method is faster than using a sort with a randomizing compare
function, since it scrambles the array
into random order in a single pass through the array
*/
- (void) scrambleArray: (NSMutableArray*) theArray;
{
int index, swapIndex;
int size = (int)[theArray count];
for (index = 0; index<size; index++)
{
swapIndex = arc4random() % size;
if (swapIndex != index)
{
[theArray exchangeObjectAtIndex: index withObjectAtIndex: swapIndex];
}
}
}
/*
randomArrayOfSize: Create and return a NSMutableArray of NSNumbers,
scrambled into random order.
This method returns an autoreleased array. If you want to keep
it, save it to a retained property.
*/
-(NSMutableArray*) randomArrayOfSize: (NSInteger) size;
{
NSMutableArray* result = [NSMutableArray arrayWithCapacity:size];
int index;
for (index = 0; index<size; index++)
[result addObject: [NSNumber numberWithInt: index]];
[self scrambleArray: result];
currentIndex = 0; //This is an instance variable.
return result;
}
- (void) testRandomArray
{
NSInteger size = 20;
int index;
NSInteger randomValue;
NSMutableArray* randomArray = [self randomArrayOfSize: size];
for (index = 0; index< size; index++)
{
randomValue = [[randomArray objectAtIndex: currentIndex] intValue];
NSLog(@"Random value[%d] = %ld", index, randomValue);
currentIndex++;
if (currentIndex >= size)
{
NSLog(@"At end of array. Scrambling the array again.");
[self scrambleArray: randomArray];
}
}
}
теперь я хочу иметь возможность получать данные в randomArray из других моих методов.
Спасибо,
Шломи