Objective-C,我如何产生一个数组/字符串列表并为每个字符串计数? [英] Objective-C, How can I produce an array / list of strings and count for each?

查看:40
本文介绍了Objective-C,我如何产生一个数组/字符串列表并为每个字符串计数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我的目的是产生一个数组,可以用来为UITableView添加节标题.我认为最简单的方法是生成一个sections数组.

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.

因此,在我的填充数据数组函数中,我想填充一个显示数组.因此,在记录1中查找我的显示数组中的第一个日期,如果不存在则创建一个新的数组项,如果存在则创建一个新的数组项.

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;

我的问题是如何创建和使用带有值的数组,在其中我可以将add的新元素添加到现有元素的数量中并搜索现有元素?

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 ?

推荐答案

我认为,如果我理解您的意见,那么NSMutableDictionary就可以使用.(如NR4TR所说),但是,我认为对象将是描述,而键将是计数.您可以检查按键并以相同的手势获得计数.如果objectForKey的返回值为nil,则不存在.

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

希望这不是书呆子,但我认为有一些建议会有所帮助.

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和NSMutableDictionary

NSDictionary and NSMutableDictionary

  • 每个键可容纳n个对象.(我认为...我不必测试极限,但是我知道您可以将NSSet作为值返回.)
  • KEY比INDEX更重要.我不认为字典是有条理的.他们知道一些东西,您需要问正确的问题.

NSArray和NSMutableArray

NSArray and NSMutableArray

  • 按顺序保留n个对象.
  • INDEX是最重要的信息.(您可以要求对象的索引,但是即使在这里,索引也是重要的部分)
  • 您通常将使用数组来驱动表视图,因为数组的有序性质适合.

NSSet,NSMutableSet和NSCountedSet

NSSet, NSMutableSet, and NSCountedSet

  • 没有顺序的对象的集合.

您可以使用[nsset setFromArray:myArray]之类的方法将其中任何一个更改为另一个.

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

这篇关于Objective-C,我如何产生一个数组/字符串列表并为每个字符串计数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆