• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: multidimensional arrays
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: multidimensional arrays


  • Subject: Re: multidimensional arrays
  • From: Shawn Erickson <email@hidden>
  • Date: Mon, 14 Mar 2005 11:22:22 -0800


On Mar 14, 2005, at 11:03 AM, Serge Meynard wrote:

Hi All,

I've run into a situation where it would be great to have a multidimensional NSArray or NSMutableArray. Is there such a class? If not, any suggestions on the most elegant solution?

Thanks,
Daniel

It's actually one of the major annoyances (to me anyway) of working in pure Obj-C code on this project: there is no direct support for multi-dimensional arrays, or even static arrays of PODs. Implementing an NSArray of NSArrays just feels ugly, and the syntax to access items is awful. And having to revert to good old-fashioned C-style indexing (e.g. myArray[y * rowLength + x]) is annoying. Since my arrays are part of larger objects, I end up writing an accessor that looks like [self itemAt:x:y]. I miss the simpler myArray[x][y] syntax of C++, and the STL containers, and the temptation is always there to start mixing the two languages; but I swore to myself I'd write this app in pure Obj-C so I could honestly know what it's like.

You do realize that C fully supports multi-dimensional arrays, you can do myArray[x][y] in plain C (and Objective-C is super set of C). In a couple of lines of code one could use NSData to manage the memory for the array (if you want to avoid malloc and/or use Cocoa memory management scheme), etc. and wrap it up in a multidimensional array class and go wild. With a little more work you could support sparse multidimensional arrays if memory is a concern.


If you think Cocoa is missing something file an enhancement request (countless examples of new methods/classes in Cocoa that come from such requests)...

<http://developer.apple.com/bugreporter/>

I also bet several freely available third party Objective-C classes exist that do what you want.

-Shawn

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list      (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden


  • Follow-Ups:
    • Re: multidimensional arrays
      • From: Serge Meynard <email@hidden>
References: 
 >multidimensional arrays (From: Daniel Child <email@hidden>)
 >Re: multidimensional arrays (From: Serge Meynard <email@hidden>)

  • Prev by Date: (Followup Question) Makes int from pointer without cast
  • Next by Date: Re: Obtaining the NSGraphicsContext for custom NSView
  • Previous by thread: Re: multidimensional arrays
  • Next by thread: Re: multidimensional arrays
  • Index(es):
    • Date
    • Thread