Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Handling large array in Swift

I have an array of English words that is about 275,000 elements long that I need to use for my iOS app written in Swift. However, Xcode doesn't seem to be able to handle such a large (3+ MB) file. The file will not open in Xcode, and when I attempt to compile the app, it seems to compile indefinitely and never build.

How should I handle this large amount of data?

like image 954
Isaac Wasserman Avatar asked Mar 05 '23 15:03

Isaac Wasserman


1 Answers

Don't put a huge literal array in your swift source code.

Instead, create a text file, drag that into your project as a resource, then open that and convert it into an array at runtime using components(separatedBy:).

For speed and storage efficiency you could instead write a conversion utility that reads your text file and uses components(separatedBy:) to convert it to an Array of Strings. Then you could write the array of Strings to a binary plist.

You could then drag the plist file into your project as a resource, and write code that reads the plist file into an Array at launch.

like image 80
Duncan C Avatar answered Mar 18 '23 03:03

Duncan C