gpt4 book ai didi

objective-c - 将 NSMutableDictionary 的键/值存储到另一个 NSMutableDictionary

转载 作者:行者123 更新时间:2023-11-28 20:33:27 26 4
gpt4 key购买 nike

所以我有三个像这样的 NSMutableDictionary:

.h文件

NSMutableDictionary *myContainer;
NSMutableDictionary *myD1;
NSMutableDictionary *myD2;

@property (nonatomic, retain) NSMutableDictionary *myContainer;
@property (nonatomic, retain) NSMutableDictionary *myD1;
@property (nonatomic, retain) NSMutableDictionary *myD2;

.m文件

@synthesize myContainer;
@synthesize myD1;
@synthesize myD2;

(初始化)

self.myContainer = [[NSMutableDictionary alloc] init];
self.myD1 = [[NSMutableDictionary alloc] init];
self.myD2 = [[NSMutableDictionary alloc] init];

现在我想将字典中的值或位置从 myD1 和 myD2 添加到 myContainer

伪:

[myD1 setValue:foo forKey:@"bar"];
[foo retain];

[myD2 setValue:hello forKey:@"world"];
[hello retain];

所以我的问题是如何将特定键/值添加到 myD1 和/或 myD2 到 myContainer?然后也从中检索键/值?

下面看起来像我需要的,但我是新手,我的格式不同。

这里来自 PHP,我将如何构建它:

$myContainer   = array();
$myD1 = array();
$myd2 = array();

$myD1['bar'] = 'foo';
$myD2['world'] = 'hello';

$myContainer['common_index'] = array($myD1, $myD2);

// Alternative
//$myContainer['common_index'] = array(0 => $myD1, 1 => $myD2);

// Retrieving values from $myD1
echo "Value: ".$myContainer['common_index'][0]['bar']."\n";
echo "Value: ".$myContainer['common_index'][1]['world']."\n";

// Alternative
foreach($myContainer['common_index'] as $array) {
foreach($array as $index => $value) {
echo "Index: {$index} Value: {$value} \n";
}
}

输出:

Value: foo
Value: hello
Index: bar Value: foo
Index: world Value: hello

相关:

最佳答案

将您的 myD1 myD2 字典添加到数组中并将其设置为 myContainer 字典,如下所示:

NSMutableArray *array = [NSMutableArray arrayWithObjects:myD1, myD2, nil];
[myContainer setObject:array forKey:@"common_index"];

为了检索它们:

NSMutableDictionary *myD1Retrieved = [[myContainer objectForKey:@"common_index"] objectAtIndex:0];
NSMutableDictionary *myD2Retrieved = [[myContainer objectForKey:@"common_index"] objectAtIndex:1];

关于objective-c - 将 NSMutableDictionary 的键/值存储到另一个 NSMutableDictionary,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11444894/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com