2015-11-12 31 views
0

這是我的數組包含的值:如何刪除特定的數組值而不是對象?

(
     { 
     yes = 0; 
    }, 
     { 
     yes = 1; 
    }, 
     { 
     yes = 2; 
    }, 
     { 
     yes = 3; 
    }, 
     { 
     yes = 4; 
    } 
) 

現在我想是除去= 1;

這是我的代碼

-(void)touchup:(UIButton*)click 
{ 


    dicButtonState=[[NSMutableDictionary alloc]init]; 


    if(click.selected==NO) 
    { 
     click.backgroundColor=[UIColor colorFromHexString:@"#ffc400"]; 
     click.selected=YES; 



     [dicButtonState setValue:@(click.tag) forKey:@"yes"]; 
//  [arrButtonState addObject:btntag]; 

     [arrButtonState addObject:dicButtonState]; 

    } 
#pragma mark deleting product on user deselection 
    else 
    { 

     click.backgroundColor=[UIColor grayColor]; 
     click.selected=NO; 


    } 

} 

這是我的代碼,維護用戶選擇按鈕的狀態我很存儲陣列的值。

我怎麼也刪除此,請人幫我做這個..

+0

這是一個'NSDictionaries'數組嗎? – Fonix

+0

@Fonix是..... –

+0

你是如何創建這個數組的?請添加一些代碼 – ZeMoon

回答

1

做到這一點最簡單的方法是找到合適的字典,然後使用[array removeObject:foundObject];

要找到正確的對象,您有幾個選項。最簡單的可能是

NSDictionary *foundObject; 
[array enumerateObjectsUsingBlock:^(NSDictionary *d, NSUInteger idx, BOOL *stop) { 
    if ([d[@"yes"] isEqual:@(1)]) { 
     *stop = YES; 
     foundObject = d; 
    } 
}]; 

[array removeObject:foundObject]; 

這假定array是可變的。

NSMutableArray *array = [originalArray mutableCopy]; 

originalArray = [array copy]; 

P.S.:如果不是,與這些線(根據需要進行調整的變量名)包住上述代碼您也可以保存索引而不是對找到的對象的引用,然後使用[array removeObjectAtIndex:foundIndex]。這是一個偏好問題。爲了從小型陣列中去除單個元素,性能特徵是無關緊要的。

1

您可以通過NSPredicate如果通過任何其他更換標誌做到這一點。

說,你的數據是這樣

NSArray *dataArray = @[@{@"value":@(0)},@{@"value":@(1)},@{@"value":@(2)},@{@"value":@(3)},@{@"value":@(4)}]; 

然後你可以使用NSPredicate過濾這個數組字典。

NSPredicate *predicate=[NSPredicate predicateWithFormat:@"value != %@",@(1)]; 
NSArray *filteredArray = [dataArray filteredArrayUsingPredicate:predicate]; 
相關問題