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

  • Home
  • SEARCH
  • 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 5963963
In Process

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 22, 20262026-05-22T19:20:50+00:00 2026-05-22T19:20:50+00:00

In the Beginning iPhone 4 book, the author has this code to create a

  • 0

In the Beginning iPhone 4 book, the author has this code to create a category for creating a deep copy of an NSDictionary that has an NSArray of names for each letter of the alphabet to show an example of an indexed table with a search bar.

#import "NSDictionary-MutableDeepCopy.h"
@implementation NSDictionary (MutableDeepCopy)
- (NSMutableDictionary *) mutableDeepCopy { 
    NSMutableDictionary *returnDict = [[NSMutableDictionary alloc] initWithCapacity:[self count]]; 
    NSArray *keys = [self allKeys]; 
    for (id key in keys) {
        id oneValue = [self valueForKey:key]; 
        id oneCopy = nil;
        if ([oneValue respondsToSelector:@selector(mutableDeepCopy)]) oneCopy = [oneValue mutableDeepCopy];
        else if ([oneValue respondsToSelector:@selector(mutableCopy)]) oneCopy = [oneValue mutableCopy];
        if (oneCopy == nil) 
           oneCopy = [oneValue copy];
        [returnDict setValue:oneCopy forKey:key]; 
        [oneCopy release];
    } 
    return returnDict;
} 

@end

Can someone explain the for loop logic? I’m not sure what he’s trying to do in seeing which value responds to which selector, and why it would be added to the dictionary. Thanks.

  • 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-22T19:20:51+00:00Added an answer on May 22, 2026 at 7:20 pm

    So, the for loop simply iterates through all the keys in the dictionary. Beforehand, we create a new dictionary called returnDict – this will be what we return.

    For each key in the dictionary we want to copy, we…

    1. Get the object stored for that key ([self valueForKey:key]), and save it into a variable called oneValue.

    2. If oneValue implements our mutableDeepCopy method (ie, it’s an NSDictionary) go call it, and assign the return value into a variable called oneCopy.

    3. Else, we see if oneCopy implements the mutableCopy method. If it does, we put the output into the oneCopy variable.

    4. At this point, we check to see if following steps (2) and (3) the oneCopy variable has had anything assigned to it (if (oneCopy == nil)). If it doesn’t (ie, it’s equal to nil) we can assume the object doesn’t implement either mutableDeepCopy or mutableCopy, so we instead call a plain old copy and assign its value to oneCopy.

    5. Add oneCopy into our returnDict dictionary using the original key.

    That’s the for loop – at the end of it all, we go and return the copied dictionary.

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

Sidebar

Related Questions

Many beginning programmers write code like this: sub copy_file ($$) { my $from =
I've been reading the Beginning iPhone 4 Development book and they have one tutorial
I'm really beginning to understand what people mean when they say that C++'s error
I'm at the beginning/middle of a project that we chose to implement using GWT.
my first question sorry if this is too basic. I followed the tutorial Beginning
I'm currently working through Apress's Beginning iPhone 3 Development. A standard they use in
I have a doubt that i have iPhone 3G and iPhone 4 both and
When beginning a new web project, i'm always a bit worried about removing pieces
I'm just beginning to have a look at Objective-C and Cocoa with a view
At the beginning of all my executable Python scripts I put the shebang line:

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.