- c - 在位数组中找到第一个零
- linux - Unix 显示有关匹配两种模式之一的文件的信息
- 正则表达式替换多个文件
- linux - 隐藏来自 xtrace 的命令
for(Attribute* attribute in appDelegate.attributeArray) {
attribute = [appDelegate.attributeArray objectAtIndex:z];
attri = attribute.zName;
int y = 0;
for(Row* r in appDelegate.elementsArray) {
r = [appDelegate.elementsArray objectAtIndex:y];
NSString *ele = r.language;
if([attri isEqualToString:ele]) {
NSLog(@"=================+++++++++++++++++%@ %@",attri, r.user);
[aaa insertObject:r atIndex:y]; //here i am adding the value to array
[dict setObject:aaa forKey:attri]; //here i am adding the array to dictionary
}
y++;
}
z++;
NSLog(@"============$$$$$$$$$$$$$$$$$$$$$$$++++++++++ %@",dict);
}
键在一个数组中,值在另一个数组中,值数组是对象格式。
我需要为单个键存储多个对象。 attributeArray
具有键值,elementsArray
具有对象。例如 attributeArray 可能有值
" English, French, German..."
并且 elementsArray 可能具有对象值
"<Row: 0x4b29d40>, <Row: 0x4b497a0>, <Row: 0x4e38940>, <Row: 0x4b2a070>, <Row: 0x4b29ab0>, <Row: 0x4b178a0> "
在第一个值中,我需要存储两个对象,对于第二个键,我需要存储 3 个对象,对于第三个键,我需要在字典中存储最后两个对象。
最佳答案
为了 super 简化,您可以使用以下代码:
NSArray *keys = ...;
NSArray *values = ...;
NSMutableDictionary *dict = [NSMutableDictionary dictionaryWithObjects: values forKeys: keys];
希望这对您有所帮助。
更新:
要在字典中为单个键存储多个值,只需使用 NSArray
/NSMutableArray
作为您的对象:
NSArray *keys = ...;
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
for( id theKey in keys)
{
NSMutableArray *item = [NSMutableArray array];
[item addObject: ...];
[item addObject: ...];
...
[dict setObject: item forKey: theKey];
}
如果一开始不知道key的所有值,需要一个一个的相加,可以使用下面的方法:
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
for( /*some cycling condition here */)
{
id keyToUse = ...;
id valueToAdd = ...;
id foundArray = [dict objectForKey: keyToUse];
if ( nil == foundArray )
{
foundArray = [NSMutableArray array];
[dict setObject: foundArray forKey: keyToUse];
}
[foundArray addObject: valueToAdd];
}
关于iphone - 如何将对象数组添加到以另一个数组为键的 nsmutabledictionary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8238266/
设置 我有一个 NSMutableDictionary,其中包含超过 800 个代表员工的 NSMutableDictionaries。我正在尝试实现一个搜索栏,但在使用我的词典时遇到严重问题。 在第
我有一个 NSMutableDictionary,其中的每个元素都是另一个字典。将其内容复制到另一个 NSMutableDictionary 的最佳方式是什么?我试过使用: firstDictiona
我有 2 个 NSMutableDictionaries,每个都有 5 个 NSArray, 这 2 个 NSMutableDictionaries 有完全相同的 5 个键和 NSArray,但内容不
将 NSMutableDictionary 添加到 NSMutableDictionary 是创建副本还是保留指针。 我希望能够更新第一个 NSMutableDictionary 的内容并在另一个 N
所以我有三个像这样的 NSMutableDictionary: .h文件 NSMutableDictionary *myContainer; NSMutableDictionary *myD1; NS
我想将 NSMUtableDictionary 值复制到另一个 NSMutableDictionary 中。我怎样才能做到这一点? 这是我的代码, PersonClass.h file: NSMu
仍在此处学习 Objective-C/iPhone SDK。我想我知道为什么这不起作用,但我只是想确认一下。 在awakeFromNib中,如果我使用[[NSMutableDictionary all
我在这个数组中有一个 NSArray 和 NSMutableDictionary 对象,如下所示。我想在这个数组的索引:2 处得到一个对象,并将来自以下数组的 NSMutableDictionary
要创建一个空字典,我应该调用 [NSMutableDictionary dictionary] 还是 [[NSMutableDictionary alloc] init]? 有什么区别? 最佳答案 [
对于存储在 NSMutableDictionary 中的字母表中的每个字母,我有 5 个 UIImageView。 5 而不是 1,以防万一他们想要拼写的单词需要更多相同的字母。 我正在创建这样的字母
一旦 NSMutableDictionary 已经达到对象的容量限制,当您尝试添加到它时会发生什么?这会撞出一个还是会抛出异常?当我添加一个新的字典对象时,有没有办法让它撞出最旧的字典对象? 最佳答案
我有 NSMutableDictionary *mutDic; 它加载了来自其他NSMutableDictionary的一些值从警报中我试图更新其值 - (void)alertView:(UIAler
在我的 iPhone 应用程序中,我使用整数来跟踪大量变量。我在我的 AppDelegate 文件(它是一个多 View 应用程序)中声明并初始化了它们,然后如果我在其他 View (类)中声明它们,
我有一个递归函数,可以将以前的字典添加到新字典中,但使用 addEntriesFromDictionary 会出现错误 这是我的示例代码(已编辑) func fetchAllSWApiList
我有一个应用程序可以从 Web 服务下载信息并将其缓存在内存中。具体来说,我的单例缓存类包含一个实例变量 NSMutableDictionary *memoryDirectory,它包含所有缓存数据。
我今天遇到了一个策略错误。我有一个 NSArray 和一个 NSMutableDictionary。问题是我的 NSMutableDictionary 变成了一个数组。这是代码: 标题: NSArra
我的 iOS 应用程序中有以下方法。声明: GMSMarker *marker = [places objectForKey:place]; 并不总能找到关键的对象,当我明确知道有一个对象要找到时。
我有两个 NSMutableDictionay,我们将预期的一个称为“A”,将操作结果的一个称为“B” 我想将“A”和“B”之间的差异插入“C”NSMutableDictionary。 我使用 if
我有一个日期选择器和一个 UIBarButtonItem,当按下 UIBarButtonItem 时,它会调用一个方法将所选日期添加到 NSMutableDictionary。代码如下所示: diar
我在 NSMutableDictionary 中面临非常奇怪的问题。请看下面的代码。 NSMutableDictionary *dict=[[NSMutableDictionary alloc]in
我是一名优秀的程序员,十分优秀!