NSMutableDictionary不保留值

Joh*_*oan 2 xcode objective-c

我是Objective-c的半新手,并且混淆了为什么我的NSMutableDictionary没有保留信息.我在头文件中声明我的变量:

@interface view_searchResults : UIViewController <UITableViewDataSource, UITableViewDelegate> {
    NSMutableDictionary *imageDicationary;
}

@property (nonatomic, retain) NSMutableDictionary *imageDictionary;
Run Code Online (Sandbox Code Playgroud)

然后在我的.m文件中,我有以下内容:

@synthesize imageDictionary;

-(UIImage *)getImageForURL:(NSURL*)url {

    UIImage*image;
    image = [UIImage imageWithData:[NSData dataWithContentsOfURL:url]];
    [imageDictionary setObject:image forKey:@"test"];

    if([imageDictionary objectForKey:@"test"]){

        NSLog(@"Exists");

    }

}
Run Code Online (Sandbox Code Playgroud)

显然还有其他代码支持这一点,但我可以确认正在传递URL,并且文件正在其他地方正确下载.此外,我可以确认此函数正在执行,我不是指文档中任何其他位置的NSMutableDictionary.

谢谢!

Rog*_*Rog 5

你在哪里创建NSMutable字典?如果这真的是你需要创建字典的所有代码:

@implementation view_searchResults 
- (id) init;{
    self = [super init];
    if(self) {
        imageDicationary = [NSMutableDictionary alloc] init];    // should also be released in dealloc.
    }
    return self;
}
Run Code Online (Sandbox Code Playgroud)

如果这是错误,那么你没有导致崩溃的原因是因为在objective-C中,向nil对象发送消息是有效的 - 它什么都不做.