将 NSMutableArray 转换为 NSDictionary 以便使用 objectForKey?

发布于 2024-12-25 13:48:39 字数 233 浏览 0 评论 0 原文

我有一个看起来像这样的 NSMutableArray

 {
    "@active" = false;
    "@name" = NAME1;
 },
 {
    "@active" = false;
    "@name" = NAME2;
 }

有没有办法将其转换为 NSDictionary,然后使用 objectForKey 获取名称对象的数组?我还能如何获得这些物品?

I have an NSMutableArray that looks like this

 {
    "@active" = false;
    "@name" = NAME1;
 },
 {
    "@active" = false;
    "@name" = NAME2;
 }

Is there a way to convert this to an NSDictionary and then use objectForKey to get an array of the name objects? How else can I get these objects?

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

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

发布评论

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

评论(6

冷清清 2025-01-01 13:48:39

还有一种更短的形式,由 Hubert

NSArray *allNames = [array valueForKey:@"name"];

valueForKey: 在 NSArray 上提出,通过向所有元素发送 valueForKey:givenKey 来返回一个新数组。

来自文档

valueForKey:
返回一个包含调用结果的数组
valueForKey: 在数组的每个对象上使用 key

- (id)valueForKey:(NSString *)key

参数
key 要检索的密钥。

返回值
检索到的密钥的值。

讨论
返回的数组包含每个返回 nil 的对象的 NSNull 元素。

示例:

NSArray *array = @[@{ @"active": @NO,@"name": @"Alice"},
                   @{ @"active": @NO,@"name": @"Bob"}];

NSLog(@"%@\n%@", array, [array valueForKey:@"name"]);

结果:

(
        {
        active = 0;
        name = Alice;
    },
        {
        active = 0;
        name = Bob;
    }
)
(
    Alice,
    Bob
)

There is a even shorter form then this proposed by Hubert

NSArray *allNames = [array valueForKey:@"name"];

valueForKey: on NSArray returns a new array by sending valueForKey:givenKey to all it elements.

From the docs:

valueForKey:
Returns an array containing the results of invoking
valueForKey: using key on each of the array's objects.

- (id)valueForKey:(NSString *)key

Parameters
key The key to retrieve.

Return Value
The value of the retrieved key.

Discussion
The returned array contains NSNull elements for each object that returns nil.

Example:

NSArray *array = @[@{ @"active": @NO,@"name": @"Alice"},
                   @{ @"active": @NO,@"name": @"Bob"}];

NSLog(@"%@\n%@", array, [array valueForKey:@"name"]);

result:

(
        {
        active = 0;
        name = Alice;
    },
        {
        active = 0;
        name = Bob;
    }
)
(
    Alice,
    Bob
)
情痴 2025-01-01 13:48:39

如果你想将 NSMutableArray 转换为相应的 NSDictionary,只需使用 mutableCopy

NSMutableArray *phone_list; //your Array
NSDictionary *dictionary = [[NSDictionary alloc] init];
dictionary = [phone_list mutableCopy];

If you want to convert NSMutableArray to corresponding NSDictionary, just simply use mutableCopy

NSMutableArray *phone_list; //your Array
NSDictionary *dictionary = [[NSDictionary alloc] init];
dictionary = [phone_list mutableCopy];
楠木可依 2025-01-01 13:48:39

这是一个 Dictionary 对象数组,因此要获取值,您可以:

[[myArray objectAtIndex:0]valueForKey:@"name"]; //Replace index with the index you want and/or the key.

This is an Array of Dictionary objects, so to get the values you would:

[[myArray objectAtIndex:0]valueForKey:@"name"]; //Replace index with the index you want and/or the key.
酒解孤独 2025-01-01 13:48:39

这是获取员工列表 NSMutableArray 并创建 NSMutableDictionary 的示例之一......

NSMutableArray *emloyees = [[NSMutableArray alloc]initWithObjects:@"saman",@"Ruchira",@"Rukshan",@"ishan",@"Harsha",@"Ghihan",@"Lakmali",@"Dasuni", nil];

NSMutableDictionary *dict = [NSMutableDictionary dictionary];
for (NSString *word in emloyees) {
    NSString *firstLetter = [[word substringToIndex:1] uppercaseString];
    letterList  = [dict objectForKey:firstLetter];

    if (!letterList) {
        letterList = [NSMutableArray array];
        [dict setObject:letterList forKey:firstLetter];
    }
    [letterList addObject:word];
}   NSLog(@"dic %@",dict);

This is example one of the exmple get the emplyee list NSMutableArray and create NSMutableDictionary.......

NSMutableArray *emloyees = [[NSMutableArray alloc]initWithObjects:@"saman",@"Ruchira",@"Rukshan",@"ishan",@"Harsha",@"Ghihan",@"Lakmali",@"Dasuni", nil];

NSMutableDictionary *dict = [NSMutableDictionary dictionary];
for (NSString *word in emloyees) {
    NSString *firstLetter = [[word substringToIndex:1] uppercaseString];
    letterList  = [dict objectForKey:firstLetter];

    if (!letterList) {
        letterList = [NSMutableArray array];
        [dict setObject:letterList forKey:firstLetter];
    }
    [letterList addObject:word];
}   NSLog(@"dic %@",dict);
∞梦里开花 2025-01-01 13:48:39

是的,你可以

看到这个例子:

NSDictionary *responseDictionary = [[request responseString] JSONValue];
NSMutableArray *dict = [responseDictionary objectForKey:@"data"];
NSDictionary *entry = [dict objectAtIndex:0];
NSString *num = [entry objectForKey:@"num"]; 
NSString *name = [entry objectForKey:@"name"];
NSString *score = [entry objectForKey:@"score"];

如果我不能详细说明,我很抱歉,因为我也在做一些事情

,但我希望这可以帮助你。 :)

yes you can

see this example:

NSDictionary *responseDictionary = [[request responseString] JSONValue];
NSMutableArray *dict = [responseDictionary objectForKey:@"data"];
NSDictionary *entry = [dict objectAtIndex:0];
NSString *num = [entry objectForKey:@"num"]; 
NSString *name = [entry objectForKey:@"name"];
NSString *score = [entry objectForKey:@"score"];

im sorry if i can't elaborate much because i am also working on something

but i hope that can help you. :)

傲娇萝莉攻 2025-01-01 13:48:39

不,伙计们……问题是你正在踩可可中的 KeyValue 机制。

KeyValueCoding 指定 @count 符号可以在 keyPath 中使用....

myArray.@count

SOOOOOO.... 只需切换到 ObjectForKey 就可以了!

NSMutableDictionary *myDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"theValue", @"@name", nil];

id kvoReturnedObject = [myDictionary valueForKey:@"@name"]; //WON'T WORK, the @ symbol is special in the valueForKey
id dictionaryReturnedObject = [myDictionary objectForKey:@"@name"];

NSLog(@"object = %@", dictionaryReturnedObject);

No, guys.... the problem is that you are stepping on the KeyValue Mechanism in cocoa.

KeyValueCoding specifies that the @count symbol can be used in a keyPath....

myArray.@count

SOOOOOO.... just switch to the ObjectForKey and your ok!

NSMutableDictionary *myDictionary = [NSMutableDictionary dictionaryWithObjectsAndKeys:@"theValue", @"@name", nil];

id kvoReturnedObject = [myDictionary valueForKey:@"@name"]; //WON'T WORK, the @ symbol is special in the valueForKey
id dictionaryReturnedObject = [myDictionary objectForKey:@"@name"];

NSLog(@"object = %@", dictionaryReturnedObject);
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文