Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/110.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Ios 在UIImage不工作的情况下迭代NSMutableDictionary_Ios_Objective C_Uiimage_Iteration_Nsmutabledictionary - Fatal编程技术网

Ios 在UIImage不工作的情况下迭代NSMutableDictionary

Ios 在UIImage不工作的情况下迭代NSMutableDictionary,ios,objective-c,uiimage,iteration,nsmutabledictionary,Ios,Objective C,Uiimage,Iteration,Nsmutabledictionary,我试图将从外部服务获取的图像添加到NSMutableDictionary中,并看到奇怪的结果。这就是我正在做的: - (void)fetchImages{ //Fetch Item Brand Images //self.itemBrands is an NSArray of NSDictionaries for (NSDictionary *itemBrand in self.itemBrands){ NSString *currentItemId = [itemBrand obje

我试图将从外部服务获取的图像添加到NSMutableDictionary中,并看到奇怪的结果。这就是我正在做的:

- (void)fetchImages{
//Fetch Item Brand Images
//self.itemBrands is an NSArray of NSDictionaries
for (NSDictionary *itemBrand in self.itemBrands){

    NSString *currentItemId = [itemBrand objectForKey:@"ITEM_ID"];

    //Valid Item Id. This Log message is displayed
    NSLog(@"Current Item Id: %@",currentItemId);

    NSString *currentItemImageUrl = [[IMAGE_URL stringByAppendingString:currentItemId] stringByAppendingString:@".png"];

    //Image URL is valid. This log message is displayed
    NSLog(@"Current Image URL: %@",currentItemImageUrl);

    NSURL *url = [NSURL URLWithString:currentItemImageUrl];
    NSData *data = [NSData dataWithContentsOfURL:url];
    UIImage *image = [UIImage imageWithData:data];
    if (image == nil){
        //This log message is displayed when image is not present
        NSLog(@"Image not Present 1");
    }else{
        //This log message is displayed when image is present
        NSLog(@"Image Present 1");
        [self.itemBrandImages setObject:image forKey:currentItemId];
    }

}

//This for loop is not being executed at all. No log messages displayed.   
for(id key in self.itemBrandImages){

    NSLog(@"Current Item Id2: %@",key);
    if ([self.itemBrandImages objectForKey:key] == nil){
        NSLog(@"Image Not Present 2");
    }else{
        NSLog(@"Image Present 2");
    }
}
}
我在self.itemBrandImages上迭代的第二个for循环根本没有执行。没有显示其中的任何日志消息

在将我的问题发布到此处之前,我尝试了以下操作:

1) 研究了堆栈溢出中的类似问题,并采纳了其中一个问题的建议。建议在.m文件的init方法中“执行NSMUtableDictionary的alloc init”。这也没用

2) 为了解决这个问题,我甚至尝试在NSMUtableDictionary中添加一个简单的字符串,而不是图像,但即使这样,似乎也没有保留下来

我真的很困惑,我在这里错过了什么或做错了什么。我们非常感谢您的投入

谢谢, 迈克G

也许:

for(NSString *key in [self.itemBrandImages allKeys])

我在fetchImages方法中对NSMutableDictionary进行了alloc初始化,它成功了!不确定init方法中的alloc init为什么不起作用

以下是我对本期的收获:

1) 如果您有一个数组或dictionary@property,您只是获取和设置它,而不是真正向其中添加或删除对象,那么您不需要显式地alloc init它们

2) 如果您有一个数组或dictionary@property要添加或删除对象,则需要显式地alloc init它们

我的上述陈述是真的吗?我很想听听你在这方面的意见

谢谢, 迈克

新代码:

- (void)fetchImages{
    //Fetch Item Brand Images
    self.itemBrandImages = [[NSMutableDictionary alloc] init];
    for (NSDictionary *itemBrand in self.itemBrands){

    NSString *currentItemId = [itemBrand objectForKey:@"ITEM_ID"];
    NSLog(@"Current Item Id in ItemList: %@",currentItemId);
    NSString *currentItemImageUrl = [[@"http://anythingtogo.freeiz.com/images/"stringByAppendingString:currentItemId] stringByAppendingString:@".png"];
    NSLog(@"Current Image URL in ItemList: %@",currentItemImageUrl);

    NSURL *url = [NSURL URLWithString:currentItemImageUrl];
    NSData *data = [NSData dataWithContentsOfURL:url];
    UIImage *image = [UIImage imageWithData:data];

    if (image == nil){
        NSLog(@"Image not Present 1");
    }else{
        NSLog(@"Image Present 1");
        [self.itemBrandImages setObject:@"Test" forKey:currentItemId];
    }

    }

“图像显示1”
是否打印?如果是这样的话,
self.itemBrandImages
是否与
nil
不同?可能会在两个for循环之间显示所有NSLog输出加上
NSLog(@“%@”,self.itemBrandImages)
。这没有任何区别。NSDictionary的快速枚举枚举字典键。然后他可能忘记了:
self.itemBrandImages=[NSMutableDictionary]也许吧,但在“1)研究了类似的问题……”的问题中,他声称情况并非如此。