如何用字典创建数组?

发布于 2024-11-10 07:43:24 字数 299 浏览 3 评论 0原文

我只需要一些帮助来创建一个可以存储多个字典的数组。 我有:

谢谢

NSArray * projects;
NSDictionary *project1;
NSDictionary * project2;

.h.m

projects = /* What should I write here to store the
              above two dictionaries in my array.   */

I just need some help in creating an array which can store multiple dictionaries.
I have :

.h

NSArray * projects;
NSDictionary *project1;
NSDictionary * project2;

.m

projects = /* What should I write here to store the
              above two dictionaries in my array.   */

Thanks,

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

projects = [[NSArray alloc] initWithObjects:project1, project2, nil];
projects = [[NSArray alloc] initWithObjects:project1, project2, nil];
树深时见影 2024-11-17 07:43:24

基本上(这将是正确的或不太正确,具体取决于您的实际代码、声明、属性等)。因为您将 projects 声明为 NSArray,所以您可以执行以下操作:

projects = [NSArray arrayWithObjects: project1, project2, nil];

当该变量超出范围时,它会神奇地消失,除非您保留它。

或者,如果您将 projects 声明为 NSMutableArray 并创建一个具有属性 (retain, nonatomic) 的属性,您可以执行如下操作

NSMutableArray *a = [[NSMutableArray alloc] initWithObjects:project1, project2, nil];
self.projects = a;
[a release];

:有多种方法可以实现您的目标,具体取决于您对阵列的总体需求。

Basically (and this will be right or not quite right depending on you actual code, declarations, properties, etc). Because you declared projects as an NSArray, you could do the following:

projects = [NSArray arrayWithObjects: project1, project2, nil];

This variable will go away magically when it goes out of scope unless you retain it.

Alternatively, if you declare projects as an NSMutableArray and make a property with attributes (retain, nonatomic), you could do something like this:

NSMutableArray *a = [[NSMutableArray alloc] initWithObjects:project1, project2, nil];
self.projects = a;
[a release];

There are several ways to achieve your goal, depending on your overall needs for the array.

治碍 2024-11-17 07:43:24

我不确定您正在寻找的内容是否比这更复杂,但是:

projects = [[NSArray alloc ] initWithObjects:project1,project2, nil];

应该可行。

如果您想要更复杂的东西,那么这让我知道我会修改我的答案。

I'm not sure if what you are looking for is more complex then this, but:

projects = [[NSArray alloc ] initWithObjects:project1,project2, nil];

should work.

If you are after something more complex then this let me know I will revise my answer.

儭儭莪哋寶赑 2024-11-17 07:43:24
arr = [[NSArray alloc ] initWithObjects:dict1,dict2, nil];
arr = [[NSArray alloc ] initWithObjects:dict1,dict2, nil];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文