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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 13, 20262026-06-13T00:34:40+00:00 2026-06-13T00:34:40+00:00

Possible Duplicate: How to inherit from NSDictionary? According from the API, I have to

  • 0

Possible Duplicate:
How to inherit from NSDictionary?

According from the API, I have to override the methods:

setObject:forKey:
removeObjectForKey:
initWithObjects:forKeys:
count
objectForKey:
keyEnumerator

Can you provide an example about how to override them?

  • 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-13T00:34:43+00:00Added an answer on June 13, 2026 at 12:34 am

    This is actually more complicated than merely subclassing NSMutableDictionary. NSMutableDictionary (and likewise NSMutableArray) are opaque classes. That is that they are merely interfaces that hide dynamic data structure that doesn’t conform like a normal concrete class. Specifically when you create an NSMutableDictionary, you get a class that utilizes the NSMutableDictionary interface, but is not actually an NSMutableDictionary! That means the returned class won’t actually be your class, Yikes! To subclass NSMutableDictionary is really to implement the NSMutableDictionary interface with custom functionality.

    What you’ll need to do is have your subclass have a data structure as a member variable (simplest would just be an NSMutableDictionary) and use that class for when you implement your override methods.

    example:

    @interface MyMutableDictionary : NSMutableDictionary
    
    ... interface methods ...
    
    @end
    

    and

    @implementation MyMutableDictionary
    {
        NSMutableDictionary* _proxy;
    }
    
    - (id) init {
        if (self = [super init]) {
            _proxy = [[NSMutableDictionary alloc] init];
        }
        return self;
    }
    
    ... implementation of methods ...
    
    - (void) setObject:(id)obj forKey:(id)key {
        if (obj) {
            [_proxy setObject:obj forKey:key];
        } else {
            [_proxy removeObjectForKey:key];
        }
    }
    
    @end
    

    This example is a contrived one that shows how to implement a custom NSMutableDictionary that doesn’t throw an exception when setting a nil object but instead removes the object with that key. For any remaining methods that you need to just directly use the forwardingTargetForSelector: method such that you can have your _proxy member take over any unimplemented methods (though you will need to subclass NSObject instead and explicitly declare all the desired methods you want for your mutable dictionary – this is because otherwise the real NSMutableDictionary methods will be used which won’t work).

    - (id) forwardingTargetForSelector:(SEL)aSelector
    {
        return _proxy;
    }
    
    • 0
    • Reply
    • Share
      Share
      • Share on Facebook
      • Share on Twitter
      • Share on LinkedIn
      • Share on WhatsApp
      • Report

Sidebar

Related Questions

Possible Duplicate: Unchecked exceptions in Java: Inherit from Error or RuntimeException? Looking at how
Possible Duplicate: Extracting dollar amounts from existing sql data? I have a column in
Possible Duplicate: add values to enum Why enums in Java cannot inherit from other
Possible Duplicate: How can a static class derive from an object? I have a
Possible Duplicate: How do I create a list of objects that inherit from the
Possible Duplicate: Inherit CSS class Does CSS support inheritance ? Assuming we have: .base
Possible Duplicate: What are access specifiers? Should I inherit with private, protected or public?
Possible Duplicate: Why aren't Python's superclass init methods automatically invoked? For example: class Pet(object):
Possible Duplicate: How to call base.base.method()? I have some trouble with Inheritance in C#.
Possible Duplicate: Enum “Inheritance” I have a number of classes which extend an abstract

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.