75421

Objective-C, How can I produce an array / list of strings and count for each?

Question:

My aim is to produce an array, which I can use to add section headers for a UITableView. I think the easiest way to do this, is to produce a sections array.

I want to create section headers for dates, where I'll have several or no rows for each.

So in my populate data array function, I want to populate a display array. So record 1, look for the first date in my display array, create a new array item if it doesn't exist, if it does exist add 1 to the count.

So I should end up with something like this.

arrDisplay(0).description = 1/June/2001; arrDisplay(0).value = 3; arrDisplay(1).description = 2/June/2001; arrDisplay(1).value = 0; arrDisplay(2).description = 3/June/2001; arrDisplay(2).value = 1; arrDisplay(3).description = 5/June/2001; arrDisplay(3).value = 6;

My question is how do I create and use such an array with values, where I can add new elements of add to the count of existing elements and search for existing elements ?

Answer1:

I think, if i understand you, an NSMutableDictionary would work. (as NR4TR said) but, i think the object would be the description and the key would be the count. you could check for the key and get the count in the same gesture. if the return value of objectForKey is nil, it doesn't exist.

NSMutableDictionary *tableDictionary = [[NSMutableDictionary alloc] init]; NSString *displayKey = @"1/June/2001"; NSNumber *displayCount = [tableDictionary objectForKey:displayKey]; if (displayCount != nil) { NSNumber *incrementedCount = [[NSNumber alloc] initWithInteger:[displayCount integerValue] + 1]; [tableDictionary removeObjectForKey:displayKey]; [tableDictionary setValue:incrementedCount forKey:displayKey]; [incrementedCount release]; } else { NSNumber *initialCount = [[NSNumber alloc] initWithInteger:1]; [tableDictionary setValue:initialCount forKey:displayKey]; [initialCount release]; }

<strong>EDIT:</strong> Hopefully this isn't pedantic, but I think a couple pointers will help.

Dictionaries, Sets, and Arrays all hold objects for retrieval. The manner of holding and retrieval desired drives the decision. I think of it based on the question 'what is the nature of the information that I have when I need an object being held?'

NSDictionary and NSMutableDictionary

<ul><li>Hold n objects per key. (I think...I haven't had to test a limit, but i know you can get an NSSet back as a value.)</li> <li>KEY is more important than INDEX. I don't think of dictionaries as ordered. they know something and you need to ask the correct question.</li> </ul>

NSArray and NSMutableArray

<ul><li>hold n objects in order.</li> <li>INDEX is most important bit of information. (you can ask for the index of an object but, even here, the index is the important part) </li> <li>you will typically drive table views with an array because the ordered nature of the array fits. </li> </ul>

NSSet, NSMutableSet, and NSCountedSet

<ul><li>A collection of objects without order.</li> </ul>

You can change any of these into the other with something like [nsset setFromArray:myArray];

and all of these things can hold the other as objects. I think an array as your top level is the correct thinking, but beyond that, it becomes an issue of implementation

Answer2:

Try array of dictionaries. Each dictionary contains two objects - section title and array of section rows.

Answer3:

If you want to have a description AND a rowcount then you can either create a class with those two properties and generate an NSArray of objects with that class or instead of all that you can just use an NSDictionary to store key/value lookups.

Answer4:

I think <a href="http://developer.apple.com/library/mac/documentation/Cocoa/Reference/Foundation/Classes/NSCountedSet_Class/Reference/Reference.html" rel="nofollow">NSCountedSet</a> is closest to what you want. It doesn't have an intrinsic order, but you can get an array out of it by providing a sort order.

Recommend

  • Access Google spreadsheet API without Oauth token
  • Split the data binding of KnockoutJS into table C#
  • Bootstrap tag typeahead input not populating menu with jQuery ajax
  • Typescript 2.1.5 Function calls are not supported
  • Populating a UITableView in Firebase a Set Number of Cells at a time
  • how to convert a unix timestamp into nsdate in iphone [duplicate]
  • Typeahead with bloodhound remote suggestions
  • Why won't my UITableViewCell deselect and update its text?
  • iOS Date formatting
  • What is this iOS exception with NSCFType?
  • how to solve invalid conversion specifier warning in iphone app
  • Knockout custom binding handler
  • Question about instantiating object
  • Trouble saving JSON data for use by multiple UIViews [duplicate]
  • NSScanner Loop Question
  • Row Count Is Returning the incorrect number using RaptureXML
  • Pass value from viewmodel to script in zk
  • Different response to non-authenticated users and AJAX calls
  • jQuery show() function is not executed in Safari if submit handler returns true
  • How to draw moving and Running sine wave chart using JFree chart in java?
  • Obtain ObjectIdHex value from mgo query
  • output of program is not same as passed argument
  • Incrementing object id automatically JS constructor (static method and variable)
  • Eraser for UIBezierPath
  • How to limit post in wp_query
  • Upload files with Ajax and Jquery
  • Build own AppleScript numerical error handling
  • Compare two NSDates in iPhone
  • AngularJs get employee from factory
  • NSLayoutConstraint that would pin a view to the bottom edge of a superview
  • Load html files in TinyMce
  • Java static initializers and reflection
  • Exception on Android 4.0 `android.os.StrictMode$AndroidBlockGuardPolicy.onNetwork(StrictMode)`
  • Change div Background jquery
  • Bitwise OR returns boolean when one of operands is nil
  • Is there any way to bind data to data.frame by some index?
  • Django query for large number of relationships
  • Why is Django giving me: 'first_name' is an invalid keyword argument for this function?
  • How can I use `wmic` in a Windows PE script?
  • How to push additional view controllers onto NavigationController but keep the TabBar?