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 7418933
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 29, 20262026-05-29T07:58:28+00:00 2026-05-29T07:58:28+00:00

I have an array which contains items of NSDictionary , I want to transform

  • 0

I have an array which contains items of NSDictionary, I want to transform the items to other objects, my first thought is valueForKey:, so I add a category method toMyObject for NSDictionary, and call for:

[array valueForKey:@"toMyObject"]

But it doesn’t work as expect, it just returns the array of NSNulls.

Any ideas to solve this problem if I don’t want to enumerate the array?

  • 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-05-29T07:58:28+00:00Added an answer on May 29, 2026 at 7:58 am

    Answer to myself. The valueForKey: of dictionary overwrite the default behavior, if the dictionary doesn’t have the key, it will return nil and not call the accessor method as NSObject do, as Apple document says:

    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.

    Since NSDictionary is a cluster class, it’s not recommend to subclass to overwrite the behavior. Instead I use the method swiss like this:

    @implementation NSDictionary (MyAddition)
    
    static void swizzle(Class c, SEL orig, SEL new)
    {
      Method origMethod = class_getInstanceMethod(c, orig);
      Method newMethod = class_getInstanceMethod(c, new);
      if(class_addMethod(c, orig, method_getImplementation(newMethod), method_getTypeEncoding(newMethod)))
        class_replaceMethod(c, new, method_getImplementation(origMethod), method_getTypeEncoding(origMethod));
      else
        method_exchangeImplementations(origMethod, newMethod);
    }
    
    + (void)initialize
    {
      if (self == [NSDictionary class]){
        swizzle([NSDictionary class],
                @selector(valueForKey:),
                @selector(myValueForKey:));
      }
    }
    
    - (id)toMyObject
    {
      return toMyObject;
    }
    
    ...
    
    - (id)myValueForKey:(NSString *)key
    {
      // for collection operators
      if ([key compare:@"@" options:0 range:NSMakeRange(0, 1)] == NSOrderedSame)
        return [super valueForKey:key];
    
      if ([key isEqualToString:@"toMyObject"])
        return [self toMyObject];
    
      return [self myValueForKey:key];
    }
    

    Now it’s safe for an NSArray to call valueForKey:@"toMyObject".

    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

I have an array which is filled asynchronous and contains 28 items. I want
I have a plist which contains an array of NSDictionary 's which represent a
I have an array which contains objects some may be same and some are
I have a class which contains a list of items. I want to serialize
I have an array which contains 2 types of items (lets call them type
I have UITableViewController which contains a list of items. Now, I want the list
I have variable call items having array of items which contains following value [[#<Item
I have an array of type Person, which contains string objects: FirstName, LastName, login.
I have an array which contains another array Would I notate it this way?
I have an array which contains around 50-200 hyperlinks. How can I pass this

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.