我有一个我正在学习的简单 iPhone 应用程序,我想要一个名为的实例变量urlLists
,它是NSDictionary
我已经这样声明了:
@interface MyViewController : UIViewController <UIPickerViewDataSource, UIPickerViewDelegate>{
IBOutlet UIPickerView *pickerView;
NSMutableArray *categories;
NSDictionary *urlLists;
}
@property(retain) NSDictionary *urlLists;
@end
并在实施中:
@implementation MyViewController
@synthesize urlLists;
...
- (void)viewDidLoad {
[super viewDidLoad];
categories = [[NSMutableArray alloc] init];
[categories addObject:@"Sport"];
[categories addObject:@"Entertainment"];
[categories addObject:@"Technology"];
[categories addObject:@"Political"];
NSArray *objects = [NSArray arrayWithObjects:@"value1", @"value2", @"value3", @"value4", nil];
urlLists = [NSDictionary dictionaryWithObjects:objects forKeys:categories];
for (id key in urlLists) {
NSLog(@"key: %@, value: %@", key, [urlLists objectForKey:key]);
}
}
...
@end
而且,这一切都有效到这里。
我在我的应用程序中添加了一个 UIPicker,当我选择其中一个项目时,我想在我的字典中记录所选择的项目及其相关条目。
-(void) pickerView:(UIPickerView *)thePickerView didSelectRow:(NSInteger)row inComponent:(NSInteger) component {
for (id key in self.urlLists) {
NSLog(@"key: %@, value: %@", key, [urlLists objectForKey:key]);
}
}
但我得到旧的 EXC_BAD_ACCESS 错误......
我知道我错过了一些小东西,但是什么?
谢谢
编辑::
刚刚修好了,我在retain
后面加了一个dictionaryWithObjects
但我不知道为什么我需要,不应该dictionaryWithObjects
创建一个自动释放对象?