我正在查看的功能:
-(void)viewDidLoad {
NSBundle *bundle = [NSBundle mainBundle];
NSString *plistPath = [bundle pathForResource:@"statedictionary" ofType:@"plist"];
NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
self.statesZips = dictionary;
[dictionary release];
NSArray *components = [self.stateZips allKeys];
NSArray *sorted = [components sortedArrayUsingSelector:@selector(compare:)];
self.States = sorted;
NSString *selectedState = [self.states objectAtIndex:0];
NSArray *array = [stateZips objectForKey: selectedState];
self.zips = array;
}
为什么先分配一个NSDictionary,然后分配给一个叫*dictionary的指针,再分配给实例变量stateZips?为什么不分配它并将其直接分配给实例变量并节省创建和释放另一个 NSDictionary 的内存?始终遵循相同的方法,包括稍后在此函数中使用 NSArray...
NSDictionary *dictionary = [[NSDictionary alloc] initWithContentsOfFile:plistPath];
self.statesZips = dictionary;
[dictionary release];
此外,这种排序将哈希表(字典)中的键按字母顺序排列。我不确定我是否理解这一行:
NSArray *sorted = [components sortedArrayUsingSelector:@selector(compare:)];