好吧,我遇到了nsarray問題,它正在從nsdictionary中添加重複的對象。如何避免在NSArray中插入重複的對象?
情況是這樣的:我有一個plist文件和一個字典數組。每本詞典的關鍵是'codigo'和'var'。
我所做的是我檢查每個'var'值,如果它是< 0,並且它是'codigo','var'將使用NSAttributedStrings獲取顏色。
問題出現在我遍歷數組並將評估的'var'添加到新數組中。我得到最終數組中的重複對象。
編輯:方案從xlc0212的回答得出,並通過聊天幫助:
_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];
}
arrayAtivos中的對象的順序是否重要? –
@PeterM Nope !!! –