简体   繁体   English

iOS NSSet / NSArray / NSDictionary有多大?

[英]iOS how large can an NSSet/NSArray/NSDictionary be?

I feel I should first describe what I'm trying to do and then I'll ask my question(s). 我觉得我应该首先描述我想要做的事情,然后我会问我的问题。

Background 背景

I have a large amount of words (could be up to 5,000). 我有很多单词(最多可达5,000)。 I want to be able to display a random 3 of these words on the screen and when the user presses a button, it will display another 3 random words, but without duplicates (ie without presenting the same word to the user again). 我希望能够在屏幕上随机显示这些单词中的3个,当用户按下按钮时,它将显示另外3个随机单词,但没有重复(即不再向用户显示相同的单词)。 This will loop until a timer runs out. 这将循环直到计时器用完。

Possible solution 可能解决方案

If the number of words was less, I'd just add these into an NSMutableSet, use -anyObject 3 times to get the words, and then remove the 3 words from the set each time so they're not used again when I next call -anyObject. 如果单词数量较少,我只需将它们添加到NSMutableSet中,使用-anyObject 3次获取单词,然后每次从集合中删除3个单词,这样下次调用时它们就不再使用了-anyObject。 The problem with this is I don't know if I can have a set with 5,000 NSStrings in it in iOS. 这个问题是我不知道我是否可以在iOS中拥有5,000个NSStrings。

Question

So my questions are 所以我的问题是

  1. Can I have a collection object (NSSet, NSArray, NSDictionary) with about 5,000 strings in it in iOS without any performance problems? 我可以在iOS中拥有一个包含大约5,000个字符串的集合对象(NSSet,NSArray,NSDictionary)而没有任何性能问题吗?
  2. If not how would I go about reading a subset of strings from a file to get an acceptable array size and then pull out more when I've emptied that array? 如果不是,我将如何从文件中读取字符串的子集以获得可接受的数组大小,然后在我清空该数组时拉出更多?
  3. What would be the best way to store these strings? 存储这些字符串的最佳方法是什么? They're only words, so not very long ones. 他们只是单词,所以不是很长。 I was thinking of just adding them to a file with a string on each line and reading them into a collection object on application load. 我只想将它们添加到每行上带有字符串的文件中,并在应用程序加载时将它们读入集合对象。

Thanks in advance. 提前致谢。

Just to have an idea of which memory dimensions we are looking at: 5000 words of 8 letters each can be represented in UTF-8 (8 bits per letter) with 只是想知道我们正在查看哪些内存维度:每个8个字母的5000个单词可以用UTF-8(每个字母8位)表示

5000 * 8 * 8 = 320,000 bits = 40,000 bytes < 40 kB 5000 * 8 * 8 = 320,000位= 40,000字节<40 kB

Even if the overhead of a NSArray , NSSet etc. would double or triple that size (which it certainly doesn't), you'd be fine. 即使NSArrayNSSet等的开销增加一倍或三倍(当然不会),你也没关系。 An iPhone 4S has a RAM size of 1 GB, that's 1,048,576 kB, several ten thousand times the size of your object. iPhone 4S的RAM大小为1 GB,即1,048,576 kB,是对象大小的一万倍。 In short: Don't worry. 简而言之:别担心。

You could store your words in a text file, separated by spaces, and simply read them into an NSArray with 您可以将单词存储在文本文件中,用空格分隔,然后将它们读入NSArray

NSArray *testArray = [stringOfTextfileContent componentsSeparatedByString:@" "];

限制取决于您可用的内存量。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM