简体   繁体   中英

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?

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.

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.

Store it in a file Don't use array!, use Set (can sort before displaying). If you have to use array consider https://www.hackingwithswift.com/example-code/language/how-to-split-an-array-into-chunks Use multithreading for processing.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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