1

Well, I am having problems with an nsarray that it is adding duplicate objects from an nsdictionary.

The situation is this: I have a plist file with an array of dictionaries. Each dictionary have the key being 'codigo' and 'var'.

What I did is I am checking each 'var' value if it is < 0 and if it is 'codigo' and 'var' will get a color using NSAttributedStrings.

The problem comes when I iterate through the array and add the evaluated 'var' into the new array. I am getting duplicated objects in the final array.

EDIT: Solution derived from xlc0212's answer and help through chat:

_timer = [NSTimer scheduledTimerWithTimeInterval:0.020 target:self
                                        selector:@selector(time:) userInfo:nil repeats:YES];

NSDictionary *redAttrs    = @{NSForegroundColorAttributeName:[UIColor redColor]};
NSDictionary *greenAttrs  = @{NSForegroundColorAttributeName:[UIColor colorWithRed:0.118 green:0.506 blue:0.000 alpha:1.000]};
NSDictionary *orangeAttrs = @{NSForegroundColorAttributeName:[UIColor orangeColor]};

NSString *stringUm = @"";
NSString *stringDois = @"";
NSString *stringTres = @"";


arrayAtivos = [[NSMutableOrderedSet alloc] init];
NSDictionary *item = [[NSDictionary alloc]init];

for (item in array){

    NSString *alfa = [item objectForKey:@"var"];

    float fltNumber = [alfa floatValue];

    if (fltNumber < 0) {
        stringUm = [NSString stringWithFormat:@"%@ %.2f  ",[item objectForKey:@"codigo"], fltNumber];
        int strLength = [stringUm length];
        attStr = [[NSMutableAttributedString alloc] initWithString:stringUm];
        [attStr setAttributes:redAttrs range:NSMakeRange(0,strLength)];

    } else if (fltNumber > 0 ) {
        stringDois = [NSString stringWithFormat:@"%@ %.2f  ",[item objectForKey:@"codigo"], fltNumber];
        int strLength = [stringDois length];
        attStr = [[NSMutableAttributedString alloc] initWithString:stringDois];
        [attStr setAttributes:greenAttrs range:NSMakeRange(0,strLength)];

    } else if (fltNumber == 0) {
        stringTres = [NSString stringWithFormat:@"%@ %.2f  ",[item objectForKey:@"codigo"], fltNumber];
        int strLength = [stringTres length];
        attStr = [[NSMutableAttributedString alloc] initWithString:stringTres];
        [attStr setAttributes:orangeAttrs range:NSMakeRange(0,strLength)];
    }

    [arrayAtivos addObject:attStr];

}
4

3 に答える 3

8

NSMutableOrderedSetはあなたが望むものです。に置き換えるだけNSMutableArrayNSMutableOrderedSet完了です。

配列をフィルタリングする場合は、これを使用します

NSArray *array = // your array
NSMutableArray *filteredArray = [[[NSOrderedSet alloc] initWithArray: array] mutableCopy];

交換

arrayAtivos = [NSMutableArray new];

これとともに

arrayAtivos = [[NSMutableOrderedSet alloc] init];

のタイプをarrayAtivosに変更しますNSMutableOrderedSet

于 2013-01-25T00:20:44.973 に答える
4

これを正しく理解していれば、オブジェクトを配列に追加する前に簡単なチェックを行うことができます。

if (![arrayAtivos containsObject:attStr]) {
     [arrayAtivos addObject:attStr];
}
于 2013-01-25T00:14:50.070 に答える
3

それらすべてを NSSet に追加するだけで、重複は許可されません。

そこから配列を取得できますが、それが重要な場合は並べ替える必要があります。

于 2013-01-25T00:18:54.300 に答える