如何从细节制作 Mutabledictionary 以及如何将该字典添加到 MutableArray (iphone)

发布于 2024-12-01 13:09:14 字数 246 浏览 0 评论 0原文

我有一些详细信息,例如 -

对于 userone

- name-user1
纽约市

name-user2
city-oslo

现在我想使用键 namelocation 在字典中添加这些详细信息

,我想创建一个该字典的数组,

任何人都可以告诉我该怎么办?

I have some details like--

for userone-

name-user1
city-new york

name-user2
city-oslo

Now I want to add these details in a dictionary using key name and location

and I want to make an array of that dictionary

can anyone tell me what to do?

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

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

发布评论

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

评论(2

巾帼英雄 2024-12-08 13:09:14
NSArray* nameArray = [NSArray arrayWithObjects:@"abc",@"asdfds",@"sdffasd",@"sadfds",@"asdfsd",nil];
NSArray* cityArray = [NSArray arrayWithObjects:@"abasdasc",@"asadws",@"asd",@"sads",@"fsd",nil];

NSMutableArray* dictArray = [NSMutableArray arrayWithCapacity:0];

for(int i = 0; i < [nameArray count]; i++)
{
    NSMutableDictionary* dictDetails = [[NSMutableDictionary dictionaryWithCapacity:0]autorelease];
    [dictDetails setValue:[nameArray objectAtIndex:i] forKey:@"Name"];
    [dictDetails setValue:[cityArray objectAtIndex:i] forKey:@"City"];

    [dictArray addObject:dictDetails];
}

NSLog(@"array is %@",dictArray);

编辑:它之前崩溃了,因为我向 dictDetails 发送了一个释放方法。相反,仅将其保留在自动释放中。

NSArray* nameArray = [NSArray arrayWithObjects:@"abc",@"asdfds",@"sdffasd",@"sadfds",@"asdfsd",nil];
NSArray* cityArray = [NSArray arrayWithObjects:@"abasdasc",@"asadws",@"asd",@"sads",@"fsd",nil];

NSMutableArray* dictArray = [NSMutableArray arrayWithCapacity:0];

for(int i = 0; i < [nameArray count]; i++)
{
    NSMutableDictionary* dictDetails = [[NSMutableDictionary dictionaryWithCapacity:0]autorelease];
    [dictDetails setValue:[nameArray objectAtIndex:i] forKey:@"Name"];
    [dictDetails setValue:[cityArray objectAtIndex:i] forKey:@"City"];

    [dictArray addObject:dictDetails];
}

NSLog(@"array is %@",dictArray);

EDIT: it was crashing before as i had sent a release method to dictDetails. Instead keep it in autorelease only.

も让我眼熟你 2024-12-08 13:09:14
NSDictionary* dict1 = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"user1", @"new york", nil] forKeys:[NSArray arrayWithObjects:@"name", @"location", nil]];
NSDictionary* dict2 =[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"user2", @"oslo", nil] forKeys:[NSArray arrayWithObjects:@"name", @"location", nil]]; 
NSArray* dictsArray = [NSArray arrayWithObjects:dict1, dict2, nil];
NSDictionary* dict1 = [NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"user1", @"new york", nil] forKeys:[NSArray arrayWithObjects:@"name", @"location", nil]];
NSDictionary* dict2 =[NSDictionary dictionaryWithObjects:[NSArray arrayWithObjects:@"user2", @"oslo", nil] forKeys:[NSArray arrayWithObjects:@"name", @"location", nil]]; 
NSArray* dictsArray = [NSArray arrayWithObjects:dict1, dict2, nil];
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文