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.
I would strongly suggest using JSON data in the files. JSON is a very simple markup format that gives structure to text files, and lets you basically say things like title=BBQ ribs. The reason you use JSON is that Cocoa has really good JSON handling built right in. Check out this thread, it probably does exactly what you want...
How do I parse JSON with Objective-C?
Related
So... I'm new to this. Been trying to teach myself how to program since maybe April. But I've always been tach . So... disclaimer out of the way...
I'm trying to make a Magic the Gathing based app. I'm trying to use Scryfall's database as a backend (so I don't have to catalog all 20,000 cards myself). But I'm running into errors parsing the json.
I've tried following along with Hacking with Swift's video series. I've tried two main ways.
Method 1. Downloading the bulk data, saving it to the project, and parsing it locally.
Method 2. Using URLSession.
Both times I get stuck at the same spot.
if let decodedResponse = try? JSONDecoder().decode(Response.self, from: data)
Somehow that part always fails. It works ONLY if I paste (a very small) part of the json as let json = """ [{ stuff: stuff, more stuff: more stuff}]""" directly into the main .swift file. But any time I either use Bundle.main.path(forResource: " nameOfFile", ofType: "json") or URLSession it completly fails at the decode line.
Theory 1. Scryfall isn't using json that conforms to Codable?
Theory 2. My struct to hold the data isn't "catching" the decoded data correctly.
Scryfall API
hacking with Swift > Codable cheat sheet
hacking with Swift > Sending and receiving Codable data with URLSession and SwiftUI
edit: crosspost to Reddit > iOSDev
Your „Response“-Class probably isn‘t completely correct. You can use something line quicktype to generate the model class.
You can also use a JSON-Validator to validate the json from their site (https://jsonlint.com), but I think there‘s no fault from their side
Also take a look at Error-Handling from JSON-Decoder: Error handling using JSONDecoder in Swift
Without more details, I can‘t give you more help. Try posting a snippet (response model + code) so we can analyse the issue.
I've run into a problem. I've created a file and saved 1000 jsons elements in it.
I want to extract, update, delete a specific json.
I'm creating the file the normal way using FileManager.default and writing info to it using FileManager.default.createFile(atPath: itemPath, contents: data, attributes: nil)
How can I do that without extracting the entire json array and then writing it again in swift.
I'm thinking something like select interogations etc.
Would be glad if you can help me with this.
Create an index in the file header to record the start position and length of the file corresponding to JSON.
I have a JSON file like this. I have to make bold part of string which is shown in JSON. How can I make parse this JSON?
It looks to me like you would first want to use NSJSONSerialization (Or just JSONSerialization in Swift 3) to convert your JSON to an object graph. Once you've done that, you should be able to navigate to the interestLabel keys in your data and fetch those strings.
You'll then need to parse those tagged strings somehow. If the only thing you need to do is to find <b> and </b> bold tags, and no other tags will ever appear in your data then you could probably write your own code. If the strings might have other tags and/or more complex HTML structure then you might want to use an XML/HTML parser. I suggest taking a look at this tutorial: https://www.raywenderlich.com/14172/how-to-parse-html-on-ios
I am trying to parse the xml file in xcode and store data in different arrays for different keys.My xml file is like as follows:
<Colleges>
<College>
<id>1</id>
<Name>abc</Name>
<Branches>
<Branch>a</Branch>
<Branch>b</Branch>
</Branches>
</College>
..... And many colleges....
</Colleges>
Can any one please give me the sample code to parse this file and put in separate arrays. I have knowledge of delegate methods but failed to implement them. I think I am lacking some were in between.
Try this: Simple XML to NSDictionary Converter. The two files will help you to convert your XML data to NSDictionary from which you can easily get your array.
does anyone knows a file format for configuration files easy to read by humans? I want to have something like tag = value where value may be:
String
Number(int or float)
Boolean(true/false)
Array(of String values, Number values, Boolean values)
Another structure(it will be more clear what I mean in the fallowing example)
Now I use something like this:
IntTag=1
FloatTag=1.1
StringTag="a string"
BoolTag=true
ArrayTag1=[1 2 3]
ArrayTag2=[1.1 2.1 3.1]
ArrayTag3=["str1" "str2" "str3"]
StructTag=
{
NestedTag1=1
NestedTag2="str1"
}
and so on.
Parsing is easy but for large files I find it hard to read/edit in text editors. I don't like xml for the same reason, it's hard to read. INI does not support nesting and I want to be able to nest tags. I also don't want a complicated format because I will use limited kind of values as I mentioned above.
Thanks for any help.
What about YAML ? It's easy to parse, nicely structured has wide programming language support. If you don't need the full feature set, you could also use JSON.
Try YAML - is (subjectively) easy to read, allows nesting, and is relatively simple to parse.