获得值,为了它们与NSDictionary添加 [英] Getting values as in order they added with NSDictionary

查看:90
本文介绍了获得值,为了它们与NSDictionary添加的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我要从字典中填充一个选择器,我有一个字典

  NSMutableArray * type1names = [NSMutableArray arrayWithObjects : - ,AA,AB,BA,BB,BC,CB,CC,CD,DC ,@DD,@FF,nil]; 
NSMutableArray * type1points = [NSMutableArray arrayWithObjects:@ - ,@4,@3.7,@3.3,@3.0,@2.7,@2.3,@ ,@1.7,@1.3,@1,@0,nil];
NSMutableDictionary * type1 = [[NSMutableDictionary alloc] initWithObjects:type1points forKeys:type1names];

这样初始化。

   - (NSString *)当我尝试让他们回到词典中时, )pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
YOHAppDelegate * app = [UIApplication sharedApplication] .delegate;

NSString * key = [[app.pointsDictionary allKeys] objectAtIndex:row];
return key;
}

他们从字典中无条件回来。不是随机的,总是在相同的位置,但没有任何逻辑系统。



是否有一种方法来实现这个,或者应该使用多个数组?

解决方案

A Hot Licks说,NSDictionary不保存顺序。不过, Matt Gallagher演示了如何通过对NSMutableDictionary进行子类化来创建OrderedDictionary 。 p>

另一方面,你应该认真对待他的警告。但是在某种程度上,它的子类给了我们一个模式,可以不用子类化:你可以定义一个类,它包含字典和数组,但继承自NSObject。定义一个 setObjectForKey:方法,它会转发到NSDictionary并将对象添加到数组中(只是,如果它不在字典中)。添加一个转发到字典的方法 objectForKey:,转发到数组的 objectAtIndex:

I am tring to populate a picker from a dictionary, and i have a dictionary

NSMutableArray* type1names = [NSMutableArray arrayWithObjects:@"--",@"AA",@"AB",@"BA",@"BB",@"BC",@"CB",@"CC",@"CD",@"DC",@"DD",@"FF",nil];
    NSMutableArray* type1points = [NSMutableArray arrayWithObjects:@"-",@"4",@"3.7",@"3.3",@"3.0",@"2.7",@"2.3",@"2.0",@"1.7",@"1.3",@"1",@"0", nil];
    NSMutableDictionary *type1  = [[NSMutableDictionary alloc] initWithObjects:type1points forKeys:type1names];

initialized like this. The problem occurs when i try to get them back as i put them into the Dictionary with the code below.

- (NSString *)pickerView:(UIPickerView *)thePickerView titleForRow:(NSInteger)row forComponent:(NSInteger)component {
    YOHAppDelegate *app = [UIApplication sharedApplication].delegate;

    NSString *key = [[app.pointsDictionary allKeys] objectAtIndex:row];
    return key;
}

They come back from dictionary unordered. Not random, always in same positions, but without any logical system.

Is there a way to implement this, or should i use multiple arrays for this?

解决方案

A Hot Licks said, NSDictionary does not save the order. However Matt Gallagher shows how to create an OrderedDictionary by subclassing NSMutableDictionary.

On the other hand you should take his warning seriously. But the certain way, he subclasses it gives us a pattern, that can be used without subclassing: You can define a class, that holds an dictionary and an array, but inherits from NSObject. Define a setObjectForKey: method, that forwards to NSDictionary and add the object to the array (just, if it wasn't present in the dictionary). Add an method objectForKey:, that forwards to the dictionary and objectAtIndex: that forwards to the array.

这篇关于获得值,为了它们与NSDictionary添加的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆