2015-10-07 177 views
1

我在數組一樣,存儲數據..如何從數組中刪除對象,數組包含字典

while ([delegate_obj.Recordsetmaster next]) 
    { 
    NSMutableDictionary *local_DICT = [NSMutableDictionary new]; 

    NSString *str_id = [delegate_obj.Recordsetmaster stringForColumn:@"category_id"]; 
    [local_DICT setObject:str_id forKey:@"category_id"]; 

    NSString *str_name = [delegate_obj.Recordsetmaster stringForColumn:@"category_name"]; 
    [local_DICT setObject:str_name forKey:@"category_name"]; 

    NSString *str_status = [delegate_obj.Recordsetmaster stringForColumn:@"status"]; 
    [local_DICT setObject:str_status forKey:@"status"]; 

    NSString *str_image = [delegate_obj.Recordsetmaster stringForColumn:@"image"]; 
    [local_DICT setObject:str_image forKey:@"image"]; 

    [category_array addObject:local_DICT]; 
    } 

現在我想刪除陣列5索引值如何做到這一點?

+1

從'category_array'? – Paulw11

+0

[category_array removeObjectAtIndex:3];它會崩潰。 –

+1

總是一個最佳實踐 - 在刪除特定索引上的項目之前,檢查'category_array'的大小。而且,無論您迄今爲止顯示的任何代碼如何,您都需要檢查執行「while」循環的次數,因爲這將控制您的數組大小。 – Abhinav

回答

1

我認爲它只是[NSMutableArray removeObjectAtIndex:],但是我也想提供一個更直觀的實現你的代碼:

NSDictionary *keys = @{ @"category_id", @"category_name", @"status", @"image" }; 
while ([delegate_obj.Recordsetmaster next]) 
{ 
    NSMutableDictionary *local_DICT = [NSMutableDictionary new]; 
    for (NSString *key in keys) { 
     id value = [delegate_obj.Recordsetmaster stringForColumn:key]; 
     NSAssert(value, @"No value for '%@'", key); 
     [local_DICT setObject:id forKey:key]; 
    } 
    [category_array addObject:local_DICT]; 
}