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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-15 02:11:23  来源:igfitidea点击:

Remove object from NSMutableDictionary at Index

objective-ciosxcode

提问by Craig

I have a UITableViewthat is configured for a subtitle cell. When I add a cell to the TableViewI have two arrays. One for the subtitle and the other for the main label. I also have an NSMutableDictionaryso 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 UITableViewI 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 MutableDictionaryI 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.rowis a primitive type, it cannot be used as a key in NSDictionary: you need to wrap it in NSNumberin 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 NSDictionaryis an associative container, you need to use the same key in the calls of removeObjectForKeyas 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];