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];
}
NSMutableOrderedSet is what you want. Just replace NSMutableArray
with NSMutableOrderedSet
and done.
If you want to filter the array, use this
NSArray *array = // your array
NSMutableArray *filteredArray = [[[NSOrderedSet alloc] initWithArray: array] mutableCopy];
Replace
arrayAtivos = [NSMutableArray new];
with this
arrayAtivos = [[NSMutableOrderedSet alloc] init];
and change the type of arrayAtivos
to NSMutableOrderedSet