[英]NSDictionary Objective-C
I'm trying to store data in the following fashion with a string as the key and I would like an array as the value. 我正在尝试以字符串作为键以下列方式存储数据,我希望将数组作为值。
key objects 关键对象
"letters" {'a','b','c','d'} “letters”{'a','b','c','d'}
"numbers" {1,2,3,4,5,6,7} “数字”{1,2,3,4,5,6,7}
Is this possible with NSDictionary
in code? 这是否可以在代码中使用NSDictionary
? If so how would that look like? 如果是这样,那会是什么样子? I'm really confused on this. 我真的很困惑。
A simple way to do this in code (and just one of many several ways you could do it): 在代码中执行此操作的简单方法(并且只是您可以执行此操作的多种方法之一):
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
[dict setObject:[NSArray arrayWithObjects:@"a", @"b", @"c", @"d", nil] forKey:@"letters"];
[dict setObject:[NSArray arrayWithObjects:[NSNumber numberWithInt:1], [NSNumber numberWithInt:2], [NSNumber numberWithInt:3], [NSNumber numberWithInt:4], nil] forKey:@"numbers"];
That creates an NSDictionary
with an array of strings and an array of NSNumber
objects, there are other ways to create arrays, but that demonstrates a basic way to do it. 这创建了一个带有字符串数组和NSNumber
对象数组的NSDictionary
,还有其他方法可以创建数组,但这表明了一种基本的方法。
Per the comments below: 根据以下评论:
If you wanted to add items to the arrays one at a time... 如果你想一次一个地添加项目到数组...
// create the dictionary and add the letters and numbers mutable arrays
NSMutableDictionary *dict = [NSMutableDictionary dictionary];
NSMutableArray *letters = [NSMutableArray array];
NSMutableArray *numbers = [NSMutableArray array];
[dict setObject:letters forKey:@"letters"];
[dict setObject:numbers forKey:@"numbers"];
// add a letter and add a number
[[dict objectForKey:@"letters"] addObject:@"a"];
[[dict objectForKey:@"numbers"] addObject:[NSNumber numberWithInt:1]];
// This now has an NSDictionary (hash) with two arrays, one of letters and one
// of numbers with the letter 'a' in the letters array and the number 1 in
// the numbers array
Assuming you have an NSArray
called letters
and one called numbers
containing the correct values: 假设您有一个名为letters
的NSArray
和一个包含正确值的numbers
:
NSDictionary* dict = [NSDictionary dictionaryWithObjectsAndKeys:
letters, @"letters",
numbers, @"numbers",
nil
];
Make sure to retain
if you want to keep dict
around, or alternatively use alloc
and initWithObjectsAndKeys
. 如果你想保持dict
,请确保retain
,或者使用alloc
和initWithObjectsAndKeys
。
See NSDictionary API reference for more details. 有关更多详细信息,请参阅NSDictionary API参考。
You can represent them as property-lists. 您可以将它们表示为属性列表。 Property-List Documentation . 属性列表文档 。
<dict>
<key>letters</key>
<array>
<string>a</string>
<string>b</string>
<string>c</string>
<string>d</string>
</array>
<key>letters</key>
<array>
<integer>1</integer>
<integer>2</integer>
<integer>3</integer>
<integer>4</integer>
<integer>5</integer>
<integer>6</integer>
<integer>7</integer>
</array>
</dict>
For an overview of how to serialize and de-serialize your data in Cocoa, see Archives and Serialization Guide for Cocoa 有关如何在Cocoa中序列化和反序列化数据的概述,请参阅Cocoa的归档和序列化指南
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.