• 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: Two dimensional array
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Two dimensional array


  • Subject: Re: Two dimensional array
  • From: Prachi Gauriar <email@hidden>
  • Date: Thu, 13 Nov 2003 21:05:20 -0600

On Nov 13, 2003, at 6:27 PM, Jay Rimalrick wrote:

What if I wanted to float values like 1.2, 3.4 instead of the arrays? I can't seem
to get it to work with

NSArray *root;
root = [[NSArray alloc] initWithObjects:
[[NSArray alloc] initWithObjects: 1.1, 2.2, 3.3, 4.4, 5.5, nil],
[[NSArray alloc] initWithObjects: 10.10, 9.9, 8.8, 7.7, 6.6, nil],
...
nil ];


That won't work because floats are not objects. NSArray, NSDictionary, etc only can hold objects. You can wrap C data types in the NSNumber class. Translating the above into working code:

NSArray *root;
root = [[NSArray alloc] initWithObjects:
[[NSArray alloc] initWithObjects:
[NSNumber numberWithDouble:1.1],
[NSNumber numberWithDouble:2.2],
[NSNumber numberWithDouble:3.3],
[NSNumber numberWithDouble:4.4],
[NSNumber numberWithDouble:5.5],
nil],
[[NSArray alloc] initWithObjects:
[NSNumber numberWithDouble:10.10],
[NSNumber numberWithDouble:9.9],
[NSNumber numberWithDouble:8.8],
[NSNumber numberWithDouble:7.7],
[NSNumber numberWithDouble:6.6],
nil],
...
nil ];

Obviously a lot longer and a bit slower, but it works. The nice thing is that you get all the features of NSArray, and NSNumber's is a pretty useful class sometimes.

-Prachi
_______________________________________________
cocoa-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/cocoa-dev
Do not post admin requests to the list. They will be ignored.

  • Follow-Ups:
    • Re: Two dimensional array
      • From: Roarke Lynch <email@hidden>
References: 
 >Re: Two dimensional array (From: "Jay Rimalrick" <email@hidden>)

  • Prev by Date: Re: Two dimensional array
  • Next by Date: Re: What's NSError **?
  • Previous by thread: Re: Two dimensional array
  • Next by thread: Re: Two dimensional array
  • Index(es):
    • Date
    • Thread