使用If语句初始化图像

时间:2012-11-28 16:03:55

标签: ios xcode

所以我有一个字典,它在所有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:@""];

}

任何帮助都将不胜感激。

1 个答案:

答案 0 :(得分:0)

2件事。

  1. 您错过了要发送支票的对象...&gt;&gt; if(forKey:@“block ....

  2. 您需要设置有效密钥&gt;&gt; @ “块%.3zstored”

  3. 将其更改为

    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];
         }
     }