编辑和删除 NSMutablearray 中的元素
Edit and delete elements from an NSMutable array
我有一个 NSMutableArray,它的数据是字典格式的,例如:
(
{
subject = “Maths";
rollNo = "#1";
division = A;
},
{
subject = “Science”;
rollNo = “#2”;
division = A;
},
{
subject = “Maths";
rollNo = “#3”;
division = A;
},
{
subject = “Maths";
rollNo = “#4”;
division = B;
},
{
subject = “Science”;
rollNo = “#5”;
division = A;
},
{
subject = “English”;
rollNo = “#6”;
division = A;
},
)
现在我需要合并属于同一学科和部门的卷号并修改现有数组,如下所示
(
{
subject = “Maths";
rollNo = “#1,#3”;
division = A;
},
{
subject = “Science”;
rollNo = “#2,#5”;
division = A;
},
{
subject = “Maths";
rollNo = “#4”;
division = B;
},
{
subject = “English”;
rollNo = “#6”;
division = A;
},
)
This is how I'm trying to achieve it
NSMutableArray *temp =[studentDetailsArray mutableCopy];
NSMutableArray *elementToDelete = [NSMutableArray array];
for (int m=0; m<temp.count-1; m++)
{
if ([temp[m][@"subject"] isEqualToString:temp[m+1][@"subject"]] && [temp[m][@“division”] isEqualToString:temp[m+1][@“division”]])
{
[temp[m] replaceObjectAtIndex:1 withObject:@{@"rollNo":[NSString stringWithFormat:@"%@, %@",temp[m][@"rollNo"],temp[m+1][@"rollNo"]]}];
// [temp[m] setValue:[NSString stringWithFormat:@"%@, %@",temp[m][@"rollNo"],temp[m+1][@"rollNo"]]forKey:@"rollNo"];
[elementToDelete addObject:temp[m+1]];
}
}
[temp removeObjectsInArray:elementToDelete];
但是我在替换对象时遇到错误,所以我必须将它转换成 NSMutableDictionary 吗?我应该如何着手以简单的方式做到这一点?
不需要创建更多数组。
你得到错误是因为这一行:
[temp[m] replaceObjectAtIndex:1 withObject:@{@"rollNo":[NSString stringWithFormat:@"%@, %@",temp[m][@"rollNo"],temp[m+1][@"rollNo"]]}];
应该是
NSMutableDictionary *mutable = [temp[m] mutableCopy];
[mutable setObject:[NSString stringWithFormat:@"%@, %@",temp[m][@"rollNo"],temp[m+1][@"rollNo"]] forKey:@"rollNo"];
[temp replaceObjectAtIndex:m withObject:mutable];
我有一个 NSMutableArray,它的数据是字典格式的,例如:
(
{
subject = “Maths";
rollNo = "#1";
division = A;
},
{
subject = “Science”;
rollNo = “#2”;
division = A;
},
{
subject = “Maths";
rollNo = “#3”;
division = A;
},
{
subject = “Maths";
rollNo = “#4”;
division = B;
},
{
subject = “Science”;
rollNo = “#5”;
division = A;
},
{
subject = “English”;
rollNo = “#6”;
division = A;
},
)
现在我需要合并属于同一学科和部门的卷号并修改现有数组,如下所示
(
{
subject = “Maths";
rollNo = “#1,#3”;
division = A;
},
{
subject = “Science”;
rollNo = “#2,#5”;
division = A;
},
{
subject = “Maths";
rollNo = “#4”;
division = B;
},
{
subject = “English”;
rollNo = “#6”;
division = A;
},
)
This is how I'm trying to achieve it
NSMutableArray *temp =[studentDetailsArray mutableCopy];
NSMutableArray *elementToDelete = [NSMutableArray array];
for (int m=0; m<temp.count-1; m++)
{
if ([temp[m][@"subject"] isEqualToString:temp[m+1][@"subject"]] && [temp[m][@“division”] isEqualToString:temp[m+1][@“division”]])
{
[temp[m] replaceObjectAtIndex:1 withObject:@{@"rollNo":[NSString stringWithFormat:@"%@, %@",temp[m][@"rollNo"],temp[m+1][@"rollNo"]]}];
// [temp[m] setValue:[NSString stringWithFormat:@"%@, %@",temp[m][@"rollNo"],temp[m+1][@"rollNo"]]forKey:@"rollNo"];
[elementToDelete addObject:temp[m+1]];
}
}
[temp removeObjectsInArray:elementToDelete];
但是我在替换对象时遇到错误,所以我必须将它转换成 NSMutableDictionary 吗?我应该如何着手以简单的方式做到这一点?
不需要创建更多数组。 你得到错误是因为这一行:
[temp[m] replaceObjectAtIndex:1 withObject:@{@"rollNo":[NSString stringWithFormat:@"%@, %@",temp[m][@"rollNo"],temp[m+1][@"rollNo"]]}];
应该是
NSMutableDictionary *mutable = [temp[m] mutableCopy];
[mutable setObject:[NSString stringWithFormat:@"%@, %@",temp[m][@"rollNo"],temp[m+1][@"rollNo"]] forKey:@"rollNo"];
[temp replaceObjectAtIndex:m withObject:mutable];