Re: Multi dimensional arrays, indexing items
Re: Multi dimensional arrays, indexing items
- Subject: Re: Multi dimensional arrays, indexing items
- From: Itrat Khan <email@hidden>
- Date: Sat, 4 Aug 2001 12:28:08 -0400
On Friday, August 3, 2001, at 05:16 PM, Mark Wridt wrote:
But the issue I don't understand is concerning how I
would point to a specific element inside of a specific
array, for example:
[snip]
This creates output (in the log)in the form of:
Aug 03 16:01:57 attempt[3719] fields = (Bill, 6542, S,
06R, 4, 4, 32.634, E, "+", 20010625, 100, Miles)
Aug 03 16:01:57 attempt[3719] fields = (Fred, 6550, S,
06R, 4, 4, 32.634, E, "+", 20010625, 100, Miles)
...
The Problem:
I need to be able to point to and compare or calculate
an element in (C++-like) fields[i][j], that is,
"Bill" is in fields[1][1] and "6550" is fields[2][2]..
You want to store your information as a property list, not a 2-D array.
A property list takes the form:
({name = "Bill", id = "6542", }, {name = "Fred", id = "6550", })
You implement this with an NSArray of NSDictionary objects. You can
access individual elements of the array and individual fields of a given
element. For example:
[[peopleArray objectAtIndex:0] objectForKey:@"name"]
would return "Bill." If the syntax seems like more work than
peopleArray[a][b], give it a try because it has a lot of advantages over
a 2-D array. One suggestion though, use constants instead of string
literals like @"name" to prevent misspelling your key.
You could also use the .plist format for your input file. NSArray can
read and write property lists, so you'd skip the whole parsing step.
Hope that helps.
..........................................
Itrat Khan
http://www.itrat.net