Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

matthew858

macrumors member
Original poster
Apr 15, 2008
97
0
Sydney, Australia
I have been working on an update to one of my apps, which involves a string being sorted into an array in order to display a UITableView in a certain way. The code is as follows:

Code:
- (void)presortHotspotByState 
{
	for (NSString *stateKey in vicArray)
	{
		[self presortHotspotByState:stateKey];
	}
}

I am trying to add the stateKey string into multiple arrays, not just vicArray. I have tried every combination I could think of, but nothing seems to work.

Help!
 

drivefast

macrumors regular
Mar 13, 2008
128
0
it may not be only the sdk that hates you, it may be the entire programming world... :( i can count at least 3 fundamental errors in what you showed us:
- you wont be able to add a member to an array - you would have to add it to a mutable array
- a string is not a valid iterator over an array
- you call your function from within the same function with an argument, yet the function is declared as taking no arguments.

if what i said here makes no sense to you, i would kindly direct you to one of the stickies on this forum, to get a good jumpstart book. if you will say "dude, i *meant* to call my function recursively" or "yeah but my function is overloaded elsewhere", i apologize for my misunderstanding and point to the NSMutableArray examples in the sdk docs.
 

matthew858

macrumors member
Original poster
Apr 15, 2008
97
0
Sydney, Australia
it may not be only the sdk that hates you, it may be the entire programming world... :( i can count at least 3 fundamental errors in what you showed us:
- you wont be able to add a member to an array - you would have to add it to a mutable array
- a string is not a valid iterator over an array
- you call your function from within the same function with an argument, yet the function is declared as taking no arguments.

if what i said here makes no sense to you, i would kindly direct you to one of the stickies on this forum, to get a good jumpstart book. if you will say "dude, i *meant* to call my function recursively" or "yeah but my function is overloaded elsewhere", i apologize for my misunderstanding and point to the NSMutableArray examples in the sdk docs.

The main problem with what I think you are saying is that it does work fine as it is. It's only up until now that I have encountered a need to have a string in multiple arrays/mutable arrays. Can I just continue with the coding, but change the arrays to mutable arrays and it will all work nicely, or am I missing something?
 

eddietr

macrumors 6502a
Oct 29, 2006
807
0
Virginia
I think it would be helpful if you gave a little more detail about what exactly you're trying to do.

What do you mean by "string being sorted into an array"?

When you say you want to add the string to multiple arrays, what are those arrays?

Did you mean for the above method (presort...) to be called recursively?
 

matthew858

macrumors member
Original poster
Apr 15, 2008
97
0
Sydney, Australia
Little more detail time:

I have a .plist file stored full of various locations throughout Australia. I have an array for each state in Australia. Each array is named after the 8 states of Australia, in the format of "stateArray". I have 8 tab bars, each one dedicated to a state. What the array does is store data based on what state each location is under in the .plist file. Each tab bar uses the same code, with the only thing changed is the state's array. Interface builder is not used at all.

What I am trying to do in this situation is have each of the arrays sort its data by the name of the location. Currently it is being sorted by the type of location, in some random order that only the SDK knows about.

In the code I have supplied, this is the code that tells the array to sort the data by initial letter. As can be seen, this only works for the "Vic" tab bar, as it is the only array with the above code (if this makes sense). What I am trying to do is format each of the arrays so the data is listed by the first letter, not by a random way as is currently done in every table view but the one that gets its data from "vicArray". I am trying to achieve this by adding all of the arrays to the above piece of code. The arrays take their data straight from the .plist file.
 

eddietr

macrumors 6502a
Oct 29, 2006
807
0
Virginia
OK, that helps.

So vicArray is an array of locations. And you have 7 other similar arrays which are also arrays of locations.

So let's assume your arrays hold objects of class Location. If Location has a (KVC compliant) property called "name", let's say, then you can sort the array using:
Code:
- (NSArray *)sortedArrayUsingDescriptors:(NSArray *)sortDescriptors

or alternatively if you want to define a comparator method in your Location class you can use:
Code:
- (NSArray *)sortedArrayUsingSelector:(SEL)comparator

Those are the two methods I use most. Perhaps there are others also, you'd have to check the docs. But either of those two should work nicely for what you've described. Note they each return a new array, sorted as you wish.
 

matthew858

macrumors member
Original poster
Apr 15, 2008
97
0
Sydney, Australia
OK, that helps.

So vicArray is an array of locations. And you have 7 other similar arrays which are also arrays of locations.

So let's assume your arrays hold objects of class Location. If Location has a (KVC compliant) property called "name", let's say, then you can sort the array using:
Code:
- (NSArray *)sortedArrayUsingDescriptors:(NSArray *)sortDescriptors

or alternatively if you want to define a comparator method in your Location class you can use:
Code:
- (NSArray *)sortedArrayUsingSelector:(SEL)comparator

Those are the two methods I use most. Perhaps there are others also, you'd have to check the docs. But either of those two should work nicely for what you've described. Note they each return a new array, sorted as you wish.

Will this still work even though the information is coming from a string, not an array, and going into an array?
 

eddietr

macrumors 6502a
Oct 29, 2006
807
0
Virginia
Will this still work even though the information is coming from a string, not an array, and going into an array?

Well is this an array of Strings or an array of Locations?

So it sounds like you run through a plist and as you find a Location in Victoria, you add that location to vicArray, right?

So the order in which they exist in vicArray depends on the order in which you add the locations to vicArray.

But that's ok, just when you are done populating the array then sort it.

You could insert each location such that you preserve the order. But why bother? From the way you've described the it, I would just read all the locations in, file them into the 8 arrays, and then just sort each of the arrays afterwards (before you present them to the user, but after you've loaded them)
 

matthew858

macrumors member
Original poster
Apr 15, 2008
97
0
Sydney, Australia
Hang on, I missed a step which may be important. Let's go again:

I have a .plist file that has various locations throughout Australia. I have 8 NSMutableArrays that store each state's data, with the name of the Mutable arrays being in the form of "state". That is done with the following code:
Code:
	[statesDictionary setObject:[NSMutableArray array] forKey:@"NSW"];
	[statesDictionary setObject:[NSMutableArray array] forKey:@"QLD"];
	[statesDictionary setObject:[NSMutableArray array] forKey:@"Vic"];
	[statesDictionary setObject:[NSMutableArray array] forKey:@"ACT"];
	[statesDictionary setObject:[NSMutableArray array] forKey:@"SA"];
	[statesDictionary setObject:[NSMutableArray array] forKey:@"NT"];
	[statesDictionary setObject:[NSMutableArray array] forKey:@"Tas"];
	[statesDictionary setObject:[NSMutableArray array] forKey:@"WA"];


I then have normal arrays, each named in the form of "stateArray". Each array then is told what states it has to store data for. The code where this happens is the following:
Code:
	self.NSWArray = [NSArray arrayWithObjects:@"NSW", nil];
	self.WAArray = [NSArray arrayWithObjects:@"WA", nil];
	self.QLDArray = [NSArray arrayWithObjects:@"QLD", nil];
	self.NTArray = [NSArray arrayWithObjects:@"NT", nil];
	self.VicArray = [NSArray arrayWithObjects:@"Vic", nil];
	self.SAArray = [NSArray arrayWithObjects:@"SA", nil];
	self.TasArray = [NSArray arrayWithObjects:@"Tas", nil];	
	self.ACTArray = [NSArray arrayWithObjects:@"ACT", nil];

The reason I have a NSMutableArray for each state is because of originally I had just one tab bar displaying the data for each and every state in Australia. Now I wish to have a tab bar for each state. Everything is fine except for the data not being sorted the way I want to.

Each of the files for the tab bar imports the .h file containing the above arrays. Each tab bar then displays on array, subsequently only showing the data from one state in Australia.

I wish to sort the data based on an item stored in the .plist called "name". I am not fussy (although the iPhone SDK might be) where this is put in (the code that tells the UITableView to sort the data via the "name" of the location.
 

matthew858

macrumors member
Original poster
Apr 15, 2008
97
0
Sydney, Australia
Are you saying you have one TabBar with 8 TabBarItems in it? If so, is that going to be too cramped?

You are correct with the items, but I have consulted a number of users of my application and they said they prefer this as long as they can customise the tab bar, which is exactly what they can do. The tab bar works in a similar way to the iPod application.
 

dejo

Moderator emeritus
Sep 2, 2004
15,982
452
The Centennial State
You are correct with the items, but I have consulted a number of users of my application and they said they prefer this as long as they can customise the tab bar, which is exactly what they can do. The tab bar works in a similar way to the iPod application.
So how many of the eight are showing at once? The iPod app shows 5 (4 really) from a choice of 10.
 

eddietr

macrumors 6502a
Oct 29, 2006
807
0
Virginia
OK, so you have a dictionary where the keys are the state abbreviations and the value are arrays (initially empty). So those are 8 (mutable) arrays.

Then if I'm understanding you correctly, you have a separate set of 8 (what you call "normal") arrays for each state?

So when you read the plist, which set of 8 arrays are you putting the locations into? I guess it must be the arrays within statesDictionary, since the other 8 arrays are immutable.

Look for example at this:

Code:
self.NSWArray = [NSArray arrayWithObjects:@"NSW", nil];

That is an immutable array that appears to include just a state abbreviation. I don't see what purpose this could serve in your application?

Will your users be adding or deleting items from your arrays?
 

matthew858

macrumors member
Original poster
Apr 15, 2008
97
0
Sydney, Australia
The locations are being stored into the NSMutableArrays. The NSArrays get the data from the NSMutableArrays and show it in the tab bar. The only reason for this, as I described, is because the the NSArrays stored more than one state. All the NSMutableArrays really do is take the information from the .plist file and sort it into states.

At this stage, the user is not able to edit the list. The "nil" is only there out of habit, something bad I picked up from reading Apple's sample code and applying it in everyday use where is shouldn't be used. Sorry!
 

eddietr

macrumors 6502a
Oct 29, 2006
807
0
Virginia
The locations are being stored into the NSMutableArrays. The NSArrays get the data from the NSMutableArrays and show it in the tab bar. The only reason for this, as I described, is because the the NSArrays stored more than one state. All the NSMutableArrays really do is take the information from the .plist file and sort it into states.

At this stage, the user is not able to edit the list. The "nil" is only there out of habit, something bad I picked up from reading Apple's sample code and applying it in everyday use where is shouldn't be used. Sorry!

OK, I still don't understand why you have immutable arrays with just one singe element each (state abbreviation). I'd have to look at your code, but my guess is those are probably unnecessary all together.

But regardless, if at the end of the day you have 8 NSArrays which are derived from 8 NSMutableArrays, then that's exactly your opportunity to sort them.

The NSArrays get the data from the NSMutableArrays and show it in the tab bar.

So just define each NSArray as the returned value of one of the sortedArray... methods I mentioned earlier.

EDIT: Sorry, of course I meant "one" element each. It's getting late here on the east coast of the US. :)
 

eddietr

macrumors 6502a
Oct 29, 2006
807
0
Virginia
The "nil" is only there out of habit, something bad I picked up from reading Apple's sample code and applying it in everyday use where is shouldn't be used. Sorry!

Didn't catch this earlier, but as someone else pointed out, the nil is needed there because it terminates the argument list.

But I'm guessing while the nil is important, the array that you are creating there doesn't seem very useful or necessary.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.