1

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)
}
J. Steen
  • 15,470
  • 15
  • 56
  • 63
Mark Stoneham
  • 73
  • 1
  • 8

2 Answers2

0

You can create NSNumber (which is an object, but int is not) and then store it into NSMutableDictionary:

NSNumber* num = [NSNumber numberWithInt:0];

for (int i = 1; i<=100; i++) {
   [myDict setObject:num forKey:[NSString stringWithFormat:@"block%dstored",i]];
}
Dave
  • 1,081
  • 6
  • 10
  • Great. How do I call back the numbers? (Say I wanted to know what was held at position 50 for example). Also how can I alter a specific number at a certain position? (For example I wanted to change the 0 at position 20 to a 1). – Mark Stoneham Nov 27 '12 at 15:01
  • @Mark Stoneham: [myDict setObjcect:[NSNumber numberWithInt:1] forKey:[NSString stringWithFormat:@"block%dstored",20]]; – Dave Nov 27 '12 at 15:52
0

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.

NSMutableDictionary* myDict = [[NSMutableDictionary alloc] init];
for (int i = 1; i <= 100; i ++) {
    if(myDict)
    {
        [myDict setObject:[NSNumber numberWithInt:0] forKey:[NSString stringWithFormat:@"block%.3istored",i]];
    }
}
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)

EDIT: To get the value for a certain key you can use the following:

int myInt = [[myDict objectForKey:@"block050stored"] intValue];

And if you want to replace the object for a certain key it's as easy as:

[myDict setObject:[NSNumber numberWithInt:1] forKey:@"block020stored"];

Now, the %.3i tells the string to add a number (i) formatted to always be three digits long. (000, 001, 010, 099, 100)

[NSString stringWithFormat:@"block%.3istored",i]

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.

Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281
  • Okay, I think that's more along the lines of what I need. Same questions as above though: How do I call back a specific number? (Say I wanted to know what was held at position 50 for example). Also how can I alter a specific number at a certain position? (For example I wanted to change the 0 at position 20 to a 1). – Mark Stoneham Nov 27 '12 at 15:03
  • I'm not sure I understand what 'block%.3istored' means? – Mark Stoneham Nov 27 '12 at 15:05
  • 1
    It was the three digits that was confusing me, I only need to store 100 so can use 0 to 99. I've changed it to %.2i and it's working. Also, I worked out how to call the number back but with slightly different code: NSNumber *mycallednumber = [myDict objectForKey: @"block01stored"]; – Mark Stoneham Nov 27 '12 at 15:26
  • Final question then: How do I load the 'blocks.save' file back in when the App is restarted? – Mark Stoneham Nov 27 '12 at 15:30
  • @MarkStoneham This post should help you. http://stackoverflow.com/a/2709874/716216 – Mick MacCallum Nov 27 '12 at 15:32
  • Okay, I've used: --- NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *myDict = [[paths lastObject] stringByAppendingPathComponent:@"blocks.save"]; int calledNumber1 = [[myDict objectForKey:@"block001stored"] intValue]; but I get the error No visible @interface for 'NSString' declares the selector 'objectForKey:' `code` – Mark Stoneham Nov 27 '12 at 16:02
  • NSLog(@"%@",myDict) shows the file is being loaded back in but I can't get the value as per your code without getting the error in my last comment. – Mark Stoneham Nov 27 '12 at 16:32
  • @MarkStoneham That's because you are attempting to use "objectForKey" a property of NSDictionary on an object of NSString type. – Mick MacCallum Nov 27 '12 at 17:04
  • So what should I be using? I can get the value as per your code above but it doesn't work if I load the 'blocks.save' back in. – Mark Stoneham Nov 27 '12 at 17:16
  • If I use NSMutableDictionary *myDict = [[paths lastObject] stringByAppendingPathComponent:@"blocks.save"]; the App runs but crashes when I try to get a value (i.e. block001stored). – Mark Stoneham Nov 27 '12 at 18:00
  • Okay, figured it in the end: NSArray *paths = NSSearchPathForDirectoriesInDomains(NSDocumentDirectory, NSUserDomainMask, YES); NSString *documentPath = [paths objectAtIndex:0]; NSString *path = [documentPath stringByAppendingPathComponent:@"blocks.save"]; NSMutableDictionary *myDict = [[NSMutableDictionary alloc] initWithContentsOfFile:path]; NSLog(@"%@",myDict); NSNumber *callednumber = [myDict objectForKey: @"block001stored"]; NSLog( @"block001 %d", [callednumber intValue] ); – Mark Stoneham Nov 28 '12 at 08:14