Steve Steve - 9 months ago 27
Swift Question

Multiple arrays filled with individual paragraphs from text doc

OSX - Swift
So I am filling a 'Keywords.rtf file' that i have added to my Module with a bunch of keywords. Because i am a neat freak I'm trying to do this the tidiest way i can think of.

I would like to have multiple paragraphs in the text doc and each paragraph will have similar strings separated by commas.

example of textDoc

Hi, Hey, Hello

Monday, Tuesday, Today is Wednesday

Apple, I like Bananas, I eat Orange's

Is it possible to then in my viewController, store these paragraphs in arrays. such as

var greetings = ["Hi", "Hey", "Hello"]
var days = ["Monday", "Tuesday" ,"Wednesday"]
var fruits = ["Apple" , "I like Bananas" , "I eat Orange's"]

To take it a little further is there a way i can have a heading above my paragraphs in the text file so when I store them in the array i can just say something like : let greet[] = all strings separatedBy(",") that are in the paragraph following "Greetings".

The text doc would look something like

Hi, Hey, Hello
Monday, Tuesday, Today is Wednesday
Apple, I like Bananas, I eat Orange's

Let me know if I can be more specific


I would suggest you use a plist file instead.

In Xcode, create an empty plist with New File -> Resource:

enter image description here

Then populate the plist:

enter image description here

Finally, get the contents for usage in your app:

let plist = // the URL to the plist, usually you put it in your bundle
guard let content = NSDictionary(contentsOfURL: plist) as? [String:[String]] else {
    fatalError() // or anything else to handle the missing plist
guard let days = content["days"] else { fatalError() }
print(days) // ["Monday", "Tuesday", "Wednesday"]
print(days[0]) // "Monday"