2012-11-28 80 views
0

因此,我有一个字典,其中存储在所有100个位置的数字0,但我想然后使用这些0初始化100图像没有图像。稍后当不同的数字替换存储的0(即1)时图像将改变。我有以下的代码,而这一切工作,直到最后一行:使用If语句初始化图像

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 *[email protected]"mapblock"; 
    NSString *blocktocheck=[mapblock stringByAppendingFormat:@"%d",z]; 

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

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

}

任何帮助,将不胜感激。

+0

是否有一个右括号从代码中缺少什么?你有一个开放的支架上,当且之后没有结束之前。 – thegrinner

回答

0

2件事。

  1. 你错过了对象发送检查... >>如果(forKey:@ “块....

  2. 您需要设置一个有效的密钥>> @” 块%.3zstored “

将其更改为

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

    if ([myDict objectForKey:key] == nil) { 
     [myDict setObject:img forKey:key]; 
    } 
} 
+0

这给了我一个未声明的标识符'img'。另外,我想实际设置一个图像(即block1.png),我现在拥有@“”。 –