Isaac Wasserman
Isaac Wasserman

Reputation: 1541

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?

Upvotes: 4

Views: 2166

Answers (2)

Duncan C
Duncan C

Reputation: 131418

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.

Upvotes: 6

ukim
ukim

Reputation: 2465

How about put it in a file and read it at runtime? For example, put the elements in the a JSON array and store the array in a text file. Drag the file into your Xcode project, then it will be copied into the app bundle during compilation. Read the JSON array from the file and parse it at runtime.

There are many Tutorial on the internet about reading files in bundle and parse JSON data.

Upvotes: 1

Related Questions