I am using parse to create simple API service, and i want to use queries on this.
In tutorial there is a section that describes how this is done in python actually really simple.
params = urllib.urlencode({"where":json.dumps({
"playerName": "Sean Plott",
"cheatMode": False
})})
While in python it might be simple line to do this, how do I do this in objective C? Do i really first make dictionary, then JSON encode it, transform it into string and then url encode it?
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 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'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?
I am grabbing data from a RSS feed dictionary and seems to be working, but the problem is it parses the symbol instead of actual currency. I am not sure what is the best way to go about this. Here is a tiny snippet of the code. I know there is NSLocale, but not sure if that is the best way to do this.
[currencyDict valueForKey:#"currency_symbol"];
You can get the list of Currency symbols here: http://www.forex-rates.biz/currency-symbols.htm
and use below code to Replace them.
yourstring= [yourstring stringByReplacingOccurrencesOfString:#"$" withString:#"$"];
You can get a similar Answer here.
I want to do:
text.gsub('\a', "\a").gsub('\b', "\b")......gsub('\n', "\n").....gsub('\t', "\t")......gsub('\z', "\z")
I came to this code:
('a'..'z').each { |sym| text.gsub!("\\#{sym}", "\#{sym}") }
but the problem is that I can't generate "\#{sym}" here as if it were "\n" or "\t".
There unfortunately isn't a "good" way to do this. The normal case for needing this is decoding a transport format like AJAX, but those libraries just implement the correct mapping themselves, so you rarely need it in your own code. You have two options, really:
Write out the mapping yourself, as you did in your original code. One thing you could do to make it more readable would be to create a dictionary and loop over that rather than chaining gsubs.
Use eval to create a string. For example:
c = 'n'
newline = eval "\"\\#{c}\""