I'm looking for the best way (or easiest) to import data into my iOS app using Swift. I've got a file containing recipes and I have to read in the recipe names and instructions. Example:
Fudge brownies
- Mix ingredients in processors until consistent.
- Prepare baking sheet with coconut oil and set over at 425. ....
So I have to import several dozen recipes, my questions are
Would it be best to read in a text file?
If so how is this done in Swift?
Also how do I avoid issues with reading the title and recipe into separate variables?
You can read in a text file quite easily doing something like this:
let path = NSBundle.mainBundle().pathForResource("fileName", ofType: "txt")
var dataString = String(contentsOfFile: path!, encoding: NSUTF8StringEncoding, error: nil)
Note you'll have to import Foundation
.
You could then create a method to parse the dataString, something like
func parseDataString(string: String)
which you could send the dataString to.
You could put markers (e.g. special characters like (*) ) in the text file that would allow this method to figure out where the titles end and the directions start. There are a number of ways it could be done.
You could then persist your data using CoreData.
Collected from the Internet
Please contact [email protected] to delete if infringement.
Comments