0

I have extracted OSX English language dictionary and want to use it in my Swift iPhone app. It has about 236,000 words which I have added to a swift string array.

When I try to run the build, it takes a long time to compile and then throws Segmentation Fault 11

Is this because the array is too big?

Am I going the correct path trying to add english dictionary in my project?

11
  • Can you post your code here? Especially the for loop part Commented Jul 9, 2015 at 14:12
  • Is the English dictionary part of your source code or do you add it as a resource and read it from the file system? Commented Jul 9, 2015 at 14:14
  • An average word in English has 7 characters. 236K words take 1.5MB. That's not too big, even on iOS devices. Commented Jul 9, 2015 at 14:14
  • I havn't even implemented a for loop yet. I am just declaring the array with 236,000 word strings in it. It is in the swift file not a separate resource file. Commented Jul 9, 2015 at 14:17
  • The swift file is about 2.5MB Commented Jul 9, 2015 at 14:19

2 Answers 2

1

You should probably not store this as a single string. There are more efficient data structures that you can use, such as a trie. You should also consider not loading the entire content into memory at one point but be able to navigate it from the filesystem.

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

1 Comment

As I answered myself, I was able to solve the issue but looping through array still takes some 0.2 sec or so which is slow for the purpose of my app. I like the trie idea but this concept is new to me and I do not know how to implement it in SWIFT. Will I need an additional framework or extension to accomplish it?
1

I was able to solve this problem by adding the actual dictionary text file into my xcode project. then utilize below code to fill words from the file to an array. it was pretty fast.

let path = NSBundle.mainBundle().pathForResource("dict2", ofType: "txt")
let dico = String(contentsOfFile: path!, encoding: NSUTF8StringEncoding, error: nil)
let dict = dico!.componentsSeparatedByString("\n")

Hope it helps someone.

2 Comments

I am facing a similar problem @Kashif is "dict2" a supporting file "dict2.txt"?
I just dragged dict2.txt into my project and made sure it target was selected

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.