iPhone SDK Doesn't Like Me - Arrays Being Mean

Discussion in 'iOS Programming' started by matthew858, Jan 25, 2009.

  1. matthew858 macrumors member

    Joined:
    Apr 15, 2008
    Location:
    Sydney, Australia
    #1
    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!
     
  2. drivefast macrumors regular

    Joined:
    Mar 13, 2008
    #2
    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.
     
  3. matthew858 thread starter macrumors member

    Joined:
    Apr 15, 2008
    Location:
    Sydney, Australia
    #3
    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?
     
  4. eddietr macrumors 6502a

    Joined:
    Oct 29, 2006
    Location:
    Virginia
    #4
    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?
     
  5. matthew858 thread starter macrumors member

    Joined:
    Apr 15, 2008
    Location:
    Sydney, Australia
    #5
    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.
     
  6. eddietr macrumors 6502a

    Joined:
    Oct 29, 2006
    Location:
    Virginia
    #6
    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.
     
  7. matthew858 thread starter macrumors member

    Joined:
    Apr 15, 2008
    Location:
    Sydney, Australia
    #7
    Will this still work even though the information is coming from a string, not an array, and going into an array?
     
  8. eddietr macrumors 6502a

    Joined:
    Oct 29, 2006
    Location:
    Virginia
    #8
    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)
     
  9. matthew858 thread starter macrumors member

    Joined:
    Apr 15, 2008
    Location:
    Sydney, Australia
    #9
    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.
     
  10. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #10
    Are you saying you have one TabBar with 8 TabBarItems in it? If so, is that going to be too cramped?
     
  11. matthew858 thread starter macrumors member

    Joined:
    Apr 15, 2008
    Location:
    Sydney, Australia
    #11
    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.
     
  12. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #12
    So how many of the eight are showing at once? The iPod app shows 5 (4 really) from a choice of 10.
     
  13. matthew858 thread starter macrumors member

    Joined:
    Apr 15, 2008
    Location:
    Sydney, Australia
    #13
    4 tabs out of a total of 8. Or if you want, 5, but one is of course "More".
     
  14. eddietr macrumors 6502a

    Joined:
    Oct 29, 2006
    Location:
    Virginia
    #14
    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?
     
  15. matthew858 thread starter macrumors member

    Joined:
    Apr 15, 2008
    Location:
    Sydney, Australia
    #15
    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!
     
  16. dejo Moderator

    dejo

    Staff Member

    Joined:
    Sep 2, 2004
    Location:
    The Centennial State
    #16
    No, the nil is something good... and necessary! Leave it out and you will get errors.
     
  17. eddietr macrumors 6502a

    Joined:
    Oct 29, 2006
    Location:
    Virginia
    #17
    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.

    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. :)
     
  18. eddietr macrumors 6502a

    Joined:
    Oct 29, 2006
    Location:
    Virginia
    #18
    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.
     

Share This Page