I want to store the same number in an array 100 times. These numbers will change later on, but I want to write an if statement using a counter to populate all 100 slots initially with the value of 0. Is there an easy way to do this?
Something like this, where ‘block01’ needs to change to ‘block02’, ‘block03’ etc.:
int block01 = 0;
NSMutableDictionary* myDict = [[NSMutableDictionary alloc] init];
if(myDict)
{
[myDict setObject:block01 forKey:@"block01stored"];
NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES);
NSString *documentPath = [paths objectAtIndex:0];
NSString *path = [documentPath stringByAppendingPathComponent:@"blocks.save"];
BOOL successfulWrite = [myDict writeToFile: path atomically: YES];
if(successfulWrite == NO)
}
This should help you. It’s a loop that will execute 99 times (1 – 100) adding zero as the object for a key formatted to include the current number.
EDIT: To get the value for a certain key you can use the following:
And if you want to replace the object for a certain key it’s as easy as:
Now, the
%.3itells the string to add a number (i) formatted to always be three digits long. (000, 001, 010, 099, 100)So the above line basically means, create a string with the words “block” and “stored” with a three digit representation of what ever the current value of the int “i” is in between them.