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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: June 16, 20262026-06-16T08:38:58+00:00 2026-06-16T08:38:58+00:00

I need to have a class, which has all methods of NSArray, which behave

  • 0

I need to have a class, which has all methods of NSArray, which behave the same way, but 2 methods are modified.

I want to override these 2 methods in my custom class:

1) countByEnumeratingWithState:objects:count:

2) objectAtIndex:

After hours of research I don’t see any reasonable way to do that, because:

  • I don’t want to use category, because not all NSArray instances should have the modified behaviour. (Plus that throws warnings)

  • I don’t want to re-write all initializers plus all arrayWith… methods + the primitive methods + implemented my own storage (because this functionality is already implemented in Cocoa, right? Why would I re-implement all the functionality of a class that is already there?)

  • If I have my custom class inherit NSObject and use NSArray as storage in an ivar, then all NSArray’s methods are not available when programming in Xcode (even if I can forward them to the NSArray ivar)

  • I had some success overwriting the method implementations on demand by using method_setImplementation(…), but still can’t figure out a way to have dynamically a class created at runtime, which then will have custom implementation of the 2 methods I mentioned.

Looking forward to your ideas! 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-06-16T08:38:59+00:00Added an answer on June 16, 2026 at 8:38 am

    Mantra: If something is hard (or seems like it requires more code than is necessary), it is likely that your design is counter to the design principals of the iOS / OS X frameworks. It may yield a better solution to revisit your design.


    To answer the original question, if you want to subclass NSArray (or NSMutableArray), you need to implement the primitive methods, no more, no less.

    The primitive methods are the methods declared in the @interface of the class itself. I.e.:

    @interface NSArray : NSObject
    - (NSUInteger)count;
    - (id)objectAtIndex:(NSUInteger)index;
    @end
    

    And for NSMutableArray:

    @interface NSMutableArray : NSArray
    - (void)addObject:(id)anObject;
    - (void)insertObject:(id)anObject atIndex:(NSUInteger)index;
    - (void)removeLastObject;
    - (void)removeObjectAtIndex:(NSUInteger)index;
    - (void)replaceObjectAtIndex:(NSUInteger)index withObject:(id)anObject;
    @end
    

    If you subclass NSMutableArray and implement the above 7 methods (the two from NSArray, too), you will have an NSMutableArray subclass that is compatible — assuming your methods are correctly implemented — with all APIs that consume mutable arrays.

    This is because of the way class clusters are designed. The public classes are abstract; are never directly instantiated. They provide a primitive interface that contains the class’s core functionality and then concrete implementations of all the other non-primtive API (save for the initializers, see below) that are implemented in terms of the primitives. Concrete, private, subclasses then override all the primitives and some of the non-primitives to provide optimal behaviors for specific configurations.

    I want to have an NSArray instance for a library I’m working on and I
    want to have it working transparently for the users of my library. Ie.
    for them should be no difference between using a normal NSArray and
    the modified class I’ll be providing. Ie. it’s a storage concern,
    which the end users should not be concerned with and the interface
    should remain the same as NSArray – therefore loosing all init methods
    is not really an option at that point.

    The initialization methods are not a part of the primitive interface to NSArray. You are adding a requirement above and beyond “make a class compatible with NSArray / NSMutableArray” as defined by the documentation. Nothing wrong with that, just pointing it out.

    The reason why this is the case is because it is exceptionally rare to subclass the collection classes to provide the kind of business logic you describe. Collections are very generic in their behavior whereas such business logic that conditionalizes collection behavior would be done in a class that manages the overall model layer object graph.

    If you really want to do this, provide an implementation of whatever init* methods you want, calling through to your wrapped generic instance as needed. There isn’t anything so special about the implementations of the initializers that you are going to lose much in doing so.

    No need to implement all of them, either. Implement one or two and @throw a descriptive exception on the rest.

    If you do decide to forward the ones that accept var-args, you can’t directly because there are no va_list accepting methods. Instead, you’ll want to convert the va_list of arguments into a language array (i.e. id[] foo = malloc(... * sizeof(id));) and pass it to initWithObjects:count:.

    Some other comments:

    • What you are doing [provide full NS*Array interface in a subclass] seems hard because it is not a common pattern and the framework designers saw no need to create a design to support it. Custom behaviors at the primitive collection levels are almost always better implemented at a higher level within the object graph. Almost always.

    • method_setImplementation() and dynamic class creation is academically interesting, but pretty much never a solution. Obviously, mucking with the NSArray or NSMutableArray classes (or the concrete implementation classes) is going to blow up the rest of the frameworks that rely upon standard behavior. Beyond that it, it is a pattern of dynamic OO composition that is not really intended to be used in Objective-C; it’ll be a pain in the ass to maintain.

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

Sidebar

Related Questions

I have a business class which I need to serialize to xml. It has
Consider the following case - I have a class (Fruits), which has some methods
I have a class which is going to need to use the strategy design
I have a form builder class which inherits from AbstractType and I need to
I have a problem to test my non activity-class which need the context of
I need clarification in calling methodologies, i have a base class and from which
I have a simple class for use on JNI, which i need to export
I have a program in which I need to store a Class object into
I have a .NET 2.0 class the properties of which are marked virtual.I need
I have xpath page.search(//table[@class='campaign']//table) which returns two tables. I need to choose only first

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.