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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 27, 20262026-05-27T23:01:10+00:00 2026-05-27T23:01:10+00:00

In a cocoa application that I’m writing, I want to create several different kinds

  • 0

In a cocoa application that I’m writing, I want to create several different kinds of “subclasses” of NSMutableDictionary. I put that word in quotes because I know that the recommended way to add methods to classes such as NSMutableDictionary is to use categories.

However, this is creating a confusing situation for me.

I want each category of NSMutableDictionary to implement a “run” method that does different things, depending on which “subclass” I’m referencing. For example, let’s say I have the following 3 categories:

NSMutableDictionary+Category0
NSMutableDictionary+Category1
NSMutableDictionary+Category2

In each one, I want a “run” method that does, for example, the following:

In category 0 …

-(void)run {
  NSLog(@"I'm running in NSMutableDictionary+Category0");
}

In category 1 …

-(void)run {
  NSLog(@"I'm running in NSMutableDictionary+Category1");
}

In category 2 …

-(void)run {
  NSLog(@"I'm running in NSMutableDictionary+Category2");
}

With these categories applied, I want to create an NSArray called, say, “myObjects” which contains a random mixture of the Category 0, Category 1, and Category 2 objects. Then, I want to do the following:

for (NSMutableDictionary* dict in myObjects) {
  [dict run];
}

… and have each of the category-specific “run” methods to be invoked.

Unless I’m doing something wrong, I can’t get this to work properly, because each category attempts to add a “run” method to NSMutableDictionary, and only one of those will be called.

Is what I’m trying to do even possible? If so, could someone point me to the appropriate docs?

Thanks in advance.


Thanks to Sean and Caleb. I was afraid that I wouldn’t be able to do this, for the reasons you outlined, but I thought I’d ask anyway, in case I was missing some knowledge about Objective C or cocoa that might help me.

I need the “parent” class to be an NSMutableDictionary for various application-specific reasons, so I will manage this as follows …

I’ll create a category that adds a single “run” method to NSMutableDictionary. I’ll use a special key in that dictionary to hold the instance of a new “MyRunnable” class with its own “run” method. I’ll subclass this MyRunnable class for all the various “run” implementations I desire, and then I’ll instantiate the appropriate subclass and put it in the NSMutableDictionary with this special key. Then, in the NSMutableDictionary’s “run” method, I’ll just reference the instance of “MyRunnable” via the special key and invoke its own “run” method.

This is another form of containment which allows my top-level class to still be an NSMutableDictionary.

Thanks again.

  • 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-27T23:01:11+00:00Added an answer on May 27, 2026 at 11:01 pm

    No matter how many categories you have on a given class, there’s still only the one class. There’s no way to use one category in one place and a different one, omitting the first, in another spot in the same project. If you add the same method to a class in three different categories, the method that will be used is one added by the last category. However, the order in which categories will be added is not defined, so you have no way of knowing which method you’ll get.

    Instead of using categories here, you should either: 1) create various subclasses of NSMutableDictionary, or 2) create a common base class that contains a mutable dictionary, and then subclass that as necessary. There’s a section on the NSMutableDictionary reference page that reads:

    There should typically be little need to subclass NSMutableDictionary.
    If you do need to customize behavior, it is often better to consider
    composition rather than subclassing.

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

Sidebar

Related Questions

The Problem I'm writing a Cocoa application and I want to raise exceptions that
I'm writing a Cocoa application that installs itself as an menulet in the menu
I am writing a Cocoa application that makes use of the ParseKit framework (
So, I'm writing a Cocoa application that needs to be able to display web
Is it true : Cocoa is an application environment that consist of several Frameworks
I am writing a Mac Cocoa application that will manipulate database files, which can
I'm writing a Cocoa application that displays the contents of an archive file in
I'm writing a Cocoa application that displays the contents of an archive file in
I am writing a Cocoa application that I keep adding buttons, views, and layers
I want to create an Application that connects to the iPhoto Library. So now

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.