开发者

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

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 ?


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];
}

EDIT: 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

  • 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.)
  • 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.

NSArray and NSMutableArray

  • hold n objects in order.
  • 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)
  • you will typically drive table views with an array because the ordered nature of the array fits.

NSSet, NSMutableSet, and NSCountedSet

  • A collection of objects without order.

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


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


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.


I think NSCountedSet 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.

0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜