4

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?

11
  • How about put it in a file and read it at runtime Commented Aug 9, 2018 at 16:50
  • @ukim how would I do that? Commented Aug 9, 2018 at 16:51
  • 3MB is not large (a single image might be much larger). There should be no difficulty here. Please show your code. Or even just a screenshot. Give us at least some idea of what you're doing. Thank you. Commented Aug 9, 2018 at 16:52
  • 1
    You should looking at Core Data and load it dynamically at runtime. Creating a 275k-element array takes both CPU time and memory space. Commented Aug 9, 2018 at 16:52
  • @matt tell Xcode that. I'm not even running any code yet. Simply adding the file to the scope of my project makes the application unresponsive. Commented Aug 9, 2018 at 16:54

2 Answers 2

6

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.

Sign up to request clarification or add additional context in comments.

Comments

1

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.

1 Comment

@matt I thought he meant he was putting the array in Swift code, so he could not compile it.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.