0

所以我有一个字典,它在所有 100 个位置存储数字 0,但我想使用这些 0 来初始化 100 个没有图像的图像。稍后,当不同的数字替换存储的 0(即 1)时,图像将发生变化。我有以下代码,直到最后一行都有效:

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);

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

for (int z = 1; z <= 100; z ++) {

    NSString *mapblock=@"mapblock";
    NSString *blocktocheck=[mapblock stringByAppendingFormat:@"%d",z];

    NSLog(@"%d", z);
    NSLog(@"%@", blocktocheck);

    {if (forKey:@"block%.3zstored" == 0) [@"%@", blocktocheck].image = [[UIImage imageNamed:@""];

}

任何帮助,将不胜感激。

4

1 回答 1

0

2 件事。

  1. 你错过了发送检查的对象... >> if (forKey:@"block....

  2. 您需要设置一个有效的密钥 >> @"block%.3zstored"

将其更改为

for (int z = 1; z <= 100; z ++) {
    NSString *mapblock=@"mapblock";
    NSString *blocktocheck=[mapblock stringByAppendingFormat:@"%d",z];
    id key = blocktocheck;

    if ([myDict objectForKey:key] == nil) {
         [myDict setObject:img forKey:key];
     }
 }
于 2012-11-28T16:19:25.743 回答