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

The Archive Base Latest Questions

Editorial Team
  • 0
Editorial Team
Asked: May 16, 20262026-05-16T17:08:07+00:00 2026-05-16T17:08:07+00:00

I am attempting to add a category to NSMutableArray to allow me to use

  • 0

I am attempting to add a category to NSMutableArray to allow me to use it as a 2D array (backed by the 1D array, with a method to convert x & y indexes into a flat index). As I am not allowed to add instance variables to a category, how should I keep track of row and column counts which are required to calculate the index into the array?

Here is the interface:

@interface NSMutableArray (TK2DMutableArray)
- (id) initWithColumns:(int) columns rows:(int) rows;
- (id) objectAtColumn:(int) col row:(int) row;
- (void) putObject:(id) object atColumn:(int) x Row:(int) y;
@end

And here is the implementation:

@implementation TK2DMutableArray

- (id) initWithColumns:(int) x rows:(int) y {
    self = [self initWithCapacity:x * y];

    return self;
}

- (id) objectAtColumn:(int) col row:(int) row {
    return [self objectAtIndex:[self indexAtColumn:col row:row]];
}

- (void) putObject:(id) object atColumn:(int) x row:(int) y {
    [self replaceObjectAtIndex:[self indexAtColumn:x row:y] withObject:object];
}

- (int) indexAtColumn:(int) col row:(int) row {
    return (col + (row * rows));
}

@end

I originally coded it up as a subclass of NSMutableArray; I now know that was a mistake having read up a little on class clusters. The problem is right there in the last method – what should I do with rows? It’d be a regular old ivar if I was subclassing.

I’m fully expecting to be told to create a new class with an NSMutableArray as a property, but I wanted to see if there was a way to do it with categories first.

Many 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-16T17:08:07+00:00Added an answer on May 16, 2026 at 5:08 pm

    On 10.6 and later (I’m not sure about iOS), you can use objc_setAssociatedObject to associate arbitrary things to an object, see this Apple doc. You can use this mechanism to “add” and ivar.

    But I think it’s better for you to create a new class with an NSMutableArray as a property. That way, you can distinguish a 1d array and a 2d array at the level of the types, so that the compiler can warn you. For example, you don’t want to accidentally use objectAtIndex: on a 2d array, right?

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

Sidebar

Related Questions

I'm attempting to use extension methods to add an operater overload to the C#
Attempting to recursively add XElements stepping through a category list. XElement dataResponse = new
I'm attempting to add some text to the directions results the Google Maps API
Attempting to insert an escape character into a table results in a warning. For
Scenario: Attempting to import many (>100), large(>1M recs) flat-files (csv). Problem: Many records are
I'm attempting to do the most simple of mappings with FluentNHibernate & Sql2005. Basically,
I am attempting add a tableadapter to a stored procedure in my SQL Server
I am attempting add some tests to an existing QT GUI application using QTest.
Im attempting to add voteup/votedown to one of my sites, however Im having a
I am attempting to add smart exception handling to a Silverlight 4 RIA application

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.