在nsmutablearray中的键中添加对象

时间:2014-03-17 00:32:37

标签: ios nsmutablearray nsmutabledictionary

我有一个叫做temparray的nsmutablearray。那个nsmutablearray有一个名为" mountains"的密钥。钥匙山内的物体是14,12,16,20,20,20。如何在钥匙内加上物体" mountains"仅适用于与@" 20"?

等字符串相等的对象

更新2:由于受欢迎的需求,我使用nslog打印出了nsmutablearray。它将向您展示我一直以来所描述的内容。

2014-03-16 21:36:16.493 Labresults[7213:60b] temparray (
        {
        mountains = "20";
        cookies = "11";
        picturid = "4";
    }
        {
        mountains = "20";
        cookies = "2";
        picturid = "6";
    }
        {
        mountains = "32";
        cookies = "2";
        picturid = "9";
    }
)

更新:这是我到目前为止所拥有的。它有效,但我必须让它更快更简单,更快速地工作。

    -(void)mountainloop {
        if (fillerstring == NULL) {
            int x = 0;
            _first = NULL;
            _second = NULL;

            NSDictionary *itemAtIndex =(NSDictionary *)[temparray objectAtIndex:x];
            _first = [itemAtIndex objectForKey:@"mountains"];
            if ([_first isEqualToString:@"20"]) {
            float a = [_first floatValue];
            k = [NSNumber numberWithFloat: a];
}     
            x = x + 2;//make +2
            if (([temparray count] < x)) {
                _allmonthdail.text = [NSString stringWithFormat:@"%.2f", _allmonthdail.text, a];
                temparray = NULL;         
            }
            else {
                x = x - 1;
                e = &x ;
                fillerstring = @"noo";       
                [self mountainloop];
            } 

        }
        else {
            int x= *(e);
            _first = NULL;

            NSDictionary *itemAtIndex =(NSDictionary *)[temparray objectAtIndex:x];
            _first = [itemAtIndex objectForKey:@"mountains"];
            if ([_first isEqualToString:@"20"]) {

            float a = [_first floatValue];
            float myfloatvalue = [k  floatValue];
            k = [NSNumber numberWithFloat: a + myfloatvalue];  
}       
            x = x + 2;
            if (([temparray count] < x)) {           
                float u = [k floatValue];
                _allmonthdail.text = [NSString stringWithFormat:@"%.2f", _allmonthdail.text, u];
                temparray = NULL;         
            }
            else {
                x = x - 1;
                e = &x ;
                fillerstring = @"noo";               
                [self mountainloop];
            }}      
    }

1 个答案:

答案 0 :(得分:1)

试试这个,

NSIndexSet *indxs = [temparray indexesOfObjectsPassingTest:^BOOL(NSDictionary *obj, NSUInteger idx, BOOL *stop) {
        return [obj[@"mountain"] isEqualToString:@"20"];
    }];

NSInteger sum = 20 * indxs.count;