使用NSMutableDictionary删除NSMutableArray中的Object

时间:2012-02-09 10:31:43

标签: iphone objective-c ios

我想使用可变字典删除Mutable数组中的对象,How?

我的代码:

Employees *Emp7 = [[Employees alloc] init];
Emp7.Number = 7;
Emp7.Name = @"Safa'";
[EmployeesArray addObject:Emp1];
[Dictionary setValue:Emp1 forKey:Emp1.Name];
删除按钮(IBAction)中的

Employees *objEmp2 = [Dic1 objectForKey:objEmp1.Name ];
[Dic1 removeObjectForKey:@"Safa'"];

2 个答案:

答案 0 :(得分:0)

请稍微清楚一下你的问题,这是你在寻找什么?

NSMutableDictionary *dict = [[NSMutableDictionary alloc] initWithObjects:values forKeys:keys];
NSMutableArray *array = [[NSMutableArray alloc] initWithObjects:@"1",@"2",@"3", nil];
[array removeObject:[dict objectForKey:someKey]];

其中'values'和'keys'都是数组,'somekey'是'keys'数组中的一个对象。 如果有帮助,请告诉我。

答案 1 :(得分:0)

测试代码:100%工作

NSMutableArray *EmployeesArray = [[NSMutableArray alloc]init];
NSMutableArray *EmployeesArrayToDelete = [[NSMutableArray alloc]init];

Employees *Emp7 = [[Employees alloc] init];
Emp7.number = @"7"; Emp7.Name = @"Safa'"; 
[EmployeesArray addObject:Emp7];

Employees *Emp3 = [[Employees alloc] init];
Emp3.number = @"7"; Emp3.Name = @"AppleVijay@facebook.com"; 
[EmployeesArray addObject:Emp3];

Employees *EmployeToDelete = [[Employees alloc] init];
EmployeToDelete.number = @"7"; 
EmployeToDelete.Name = @"Safa'"; 

NSLog(@"before delete : %@",EmployeesArray);

for (Employees *eachEmployee in EmployeesArray) {
    if ([eachEmployee.Name isEqual:EmployeToDelete.Name] && [eachEmployee.number isEqual: EmployeToDelete.number]) {
        [EmployeesArrayToDelete addObject:eachEmployee];
    }
}

if ([EmployeesArrayToDelete count]>0) {
    [EmployeesArray removeObjectsInArray:EmployeesArrayToDelete];
}

NSLog(@" After delete Employeesarray : %@",EmployeesArray);