xcode 从 NSMutableDictionary 索引处删除对象
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/13557489/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Remove object from NSMutableDictionary at Index
提问by Craig
I have a UITableView
that is configured for a subtitle cell. When I add a cell to the TableView
I have two arrays. One for the subtitle and the other for the main label. I also have an NSMutableDictionary
so that when I add the cell to the table it records an object and a key. My problem is when I want to delete the cell from the UITableView
I use this method:
我有一个UITableView
为字幕单元配置的。当我向我添加一个单元格时,TableView
我有两个数组。一个用于副标题,另一个用于主标签。我还有一个,NSMutableDictionary
以便当我将单元格添加到表格时,它会记录一个对象和一个键。我的问题是当我想从UITableView
我使用此方法中删除单元格时:
- (void)tableView:(UITableView *)tableView commitEditingStyle:(UITableViewCellEditingStyle)editingStyle forRowAtIndexPath:(NSIndexPath *)indexPath
{
if (editingStyle == UITableViewCellEditingStyleDelete) {
[myArray removeObjectAtIndex:indexPath.row];
[numberArray removeObjectAtIndex:indexPath.row];
[tableView deleteRowsAtIndexPaths:@[indexPath] withRowAnimation:UITableViewRowAnimationFade];
} else if (editingStyle == UITableViewCellEditingStyleInsert) {
// Create a new instance of the appropriate class, insert it into the array, and add a new row to the table view.
}
}
This works great for removing the objects from their associated arrays, but I also want to remove the objects from the MutableDictionary
I made. I thought that you would use somethings similar to what I used with the arrays.
这对于从关联的数组中删除对象非常有用,但我也想从MutableDictionary
我制作的对象中删除对象。我认为你会使用类似于我在数组中使用的东西。
[myArray removeObjectAtIndex:indexPath.row];
But I can't use
但是我不能用
[myDictionary removeObjectForKey:indexPath.row];
Does anyone know how I would write this?
有谁知道我会怎么写这个?
采纳答案by dasblinkenlight
Since indexPath.row
is a primitive type, it cannot be used as a key in NSDictionary
: you need to wrap it in NSNumber
in order to do so.
由于indexPath.row
是原始类型,因此不能用作 中的键NSDictionary
:您需要将其包装NSNumber
在其中才能这样做。
[myDictionary removeObjectForKey:[NSNumber numberWithInt:indexPath.row]];
This would work only if you use NSNumber
-wrapped integers as keys in your NSMutableDictionary
. In general, since NSDictionary
is an associative container, you need to use the same key in the calls of removeObjectForKey
as you used in the calls of setObject:forKey:
.
仅当您使用NSNumber
-wrapped 整数作为NSMutableDictionary
. 通常,由于NSDictionary
是一个关联容器,你需要使用相同的密钥中的电话removeObjectForKey
,你在的调用中使用setObject:forKey:
。
回答by Prasad
Use this code to delete the data from the NSMutableDictionary
使用此代码从 NSMutableDictionary 中删除数据
//First get all the keys of dictionary into one array
NSArray *sectionsArray = [mutaleDictionary allKeys];
//Get all the data of tapped section into one array by using indexpath.section
NSMutableArray *objectsAtSection = [mutaleDictionary valueForKey:[sectionsArray objectAtIndex:indexPath.section]];
//remove the particular object by using indexPath.row from the array
[objectsAtSection removeObjectAtIndex:indexPath.row];