iOS how large can an NSSet/NSArray/NSDictionary be?

ios, iphone, objective-c

Solution

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

5000 * 8 * 8 = 320,000 bits = 40,000 bytes < 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. 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. 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 *testArray = [stringOfTextfileContent componentsSeparatedByString:@" "];

Problem

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). 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 (i.e. without presenting the same word to the user again). 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. The problem with this is I don't know if I can have a set with 5,000 NSStrings in it in iOS. Question So my questions are - Can I have a collection object (NSSet, NSArray, NSDictionary) with about 5,000 strings in it in iOS without any performance problems? - 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? - 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.

Original source

Related problems