Sign Up

Sign Up to our social questions and Answers Engine to ask questions, answer people’s questions, and connect with other people.

Have an account? Sign In

Have an account? Sign In Now

Sign In

Login to our social questions & Answers Engine to ask questions answer people’s questions & connect with other people.

Sign Up Here

Forgot Password?

Don't have account, Sign Up Here

Forgot Password

Lost your password? Please enter your email address. You will receive a link and will create a new password via email.

Have an account? Sign In Now

You must login to ask a question.

Forgot Password?

Need An Account, Sign Up Here

Please briefly explain why you feel this question should be reported.

Please briefly explain why you feel this answer should be reported.

Please briefly explain why you feel this user should be reported.

Sign InSign Up

The Archive Base

The Archive Base Logo The Archive Base Logo

The Archive Base Navigation

  • SEARCH
  • Home
  • About Us
  • Blog
  • Contact Us
Search
Ask A Question

Mobile menu

Close
Ask a Question
  • Home
  • Add group
  • Groups page
  • Feed
  • User Profile
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Buy Points
  • Users
  • Help
  • Buy Theme
  • SEARCH
Home/ Questions/Q 8068793
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 5, 20262026-06-05T12:48:34+00:00 2026-06-05T12:48:34+00:00

Aim: To obtain an NSArray containing unique keys for given NSDictionary(s) using elegant code

  • 0

Aim: To obtain an NSArray containing unique keys for given NSDictionary(s) using elegant code

Example Code with Current Working Solution:

NSArray *data = [[NSArray alloc] initWithObjects:
                 [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:1], @"a", [NSNumber numberWithInt:2], @"b", nil],
                 [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:3], @"b", [NSNumber numberWithInt:4], @"c", nil],
                 [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:5], @"a", [NSNumber numberWithInt:6], @"c", nil],
                 [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:7], @"b", [NSNumber numberWithInt:8], @"a", nil],
                 [NSDictionary dictionaryWithObjectsAndKeys:[NSNumber numberWithInt:8], @"c", [NSNumber numberWithInt:9], @"b", nil],
                 nil];

// create an NSArray of all the dictionary keys within the NSArray *data
NSMutableSet *setKeys = [[NSMutableSet alloc] init]; 
for (int i=0; i<[data count]; i++) {
    [setKeys addObjectsFromArray:[[data objectAtIndex:i] allKeys]];
}
NSArray *arrayKeys = [setKeys allObjects];
NSLog(@"arrayKeys: %@", arrayKeys);

Which returns the desired array of keys:

2012-06-11 16:52:57.351 test.kvc[6497:403] arrayKeys: (
    a,
    b,
    c
)

Question: Is there a more elegant way of approaching this? Surely there must be some KVC approach that can get all the keys without having to iterate through the array? I’ve been looking at Apple Developer Documentation and can’t see a solution. Any ideas? (looking at purely elegance of code rather than performance).

  • 1 1 Answer
  • 0 Views
  • 0 Followers
  • 0
Share
  • Facebook
  • Report

Leave an answer
Cancel reply

You must login to add an answer.

Forgot Password?

Need An Account, Sign Up Here

1 Answer

  • Voted
  • Oldest
  • Recent
  • Random
  1. Editorial Team
    Editorial Team
    2026-06-05T12:48:35+00:00Added an answer on June 5, 2026 at 12:48 pm

    Normally you could use KVC by doing something like this:

    NSArray *uniqueKeys = [data valueForKeyPath:@"@distinctUnionOfArrays.allKeys";
    

    However NSDictionary overrides the valueForKey: selector which is used by the KVC internals, so this will not work correctly.

    The documentation for NSDictionary’s valueForKey: method tells us that:

    If key does not start with “@”, invokes objectForKey:. If key does start with “@”, strips the “@” and invokes [super valueForKey:] with the rest of the key.

    So we just insert an @ before allKeys:

    NSArray *uniqueKeys = [data valueForKeyPath:@"@distinctUnionOfArrays.@allKeys"];
    

    And we get what we want:

    (lldb) po [data valueForKeyPath:@"@distinctUnionOfArrays.@allKeys"]
    (id) $14 = 0x07bb2fc0 <__NSArrayI 0x7bb2fc0>(
    c,
    a,
    b
    )
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I'm using authorize.net AIM, the sample code they provide prints an ordered list of
Aim : Print present working directory in c on Linux machine. Without using pointers,
I'm using System.Printing to interrogate PrintQueue status - my aim being to obtain status
Lets assume the following: string s(!Hello!'); My aim is to obtain in the s
Aim: To have the if statement execute a onclick event through code. I currently
The aim: to write a js (using jquery) that will perform 2 blinking of
My aim is to create identify a piece of code that increments a number
My overall aim is to create an editor which I can skin using jQuery
There's a status indicator code for AIM which returns two different images depending on
Aim: Calling a very slow stored procedure (with parameters) asynchronously from code behind of

Explore

  • Home
  • Add group
  • Groups page
  • Communities
  • Questions
    • New Questions
    • Trending Questions
    • Must read Questions
    • Hot Questions
  • Polls
  • Tags
  • Badges
  • Users
  • Help
  • SEARCH

Footer

© 2021 The Archive Base. All Rights Reserved
With Love by The Archive Base

Insert/edit link

Enter the destination URL

Or link to existing content

    No search term specified. Showing recent items. Search or use up and down arrow keys to select an item.