I am trying to make an app, which focuses on learning words. Right now I have a big text file, with around 4000 lines, and I want to make my app be able to read this. First I converterd the textfile to swift syntax so it becomes a dictionary (I did this with a regular text editing programme), but when I try to copy it into a .swift file, xcode stops responding, and when the text finally gets copied over, it doesn't compile until I halve the text. What is the best way to get this text file into my app?
Asked
Active
Viewed 198 times
2
-
3Hi david, welcome to Stack Overflow. You've got an interesting question here. Unfortunately, it's doesn't conform to Stack Overflows [mcve] so it will may not be received very well. For best results on SO please be sure to read [ask] before asking questions! :) – Dan Beaulieu Dec 15 '15 at 17:28
-
3You should use a plist file to store your dictionary – Leo Dabus Dec 15 '15 at 17:53
-
Xcode really doesn't like copy pasting large amounts of text, or find/replace in large amounts of text. I am also working on a project with about 5000 lines of static text. In my case it is stored in an `Array`. I finally decided to create a couple of files and store parts of the text in each. A simple function appends each part in the final array. I don't know if this is THE way to go. But it doesn't upset Xcode. – R Menke Dec 15 '15 at 17:58
-
Please see this: http://stackoverflow.com/questions/24045570/swift-read-plist, I think it should help you. – 0x416e746f6e Dec 15 '15 at 20:40
1 Answers
1
I usually don't reply to open ended questions like these but this is how I would approach your problem.
- Create your text file as a rtf or txt format (doesn't matter what you prefer)
- Add all your words line by line in that text file
- Add that text file to your Xcode resources dir. (drag and drop it to your project)
- Now what you do in your Xcode code is read each line of that file and add it into an array.
Here is some sample code to get you started. (Objective-C). Good luck buddy.
NSString* path = [[NSBundle mainBundle] pathForResource:@"my_big_text_file" ofType:@"rtf"];
NSString* content = [NSString stringWithContentsOfFile:path encoding:NSUTF8StringEncoding error:NULL];
content=[content stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceCharacterSet]];
//put this string into an array
NSArray *tempArray1 = [content componentsSeparatedByString:@"\n"];

Sam B
- 27,273
- 15
- 84
- 121