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.
Related
My question might be a bit stupid but I am a bit confused after working for the first time with iOS.
I am currently working with Swift and want to do a Post request from the background. To achieve this, I'm using Alamofire and so far its no problem to do a post request with a JSON body which contains Data that the WebApi writes into a Database but I am worried about the timelimit with a lot of Data to send.
Therefore the Web API got Methods like the following:
AddData(Data[] data){ ...}
the matching Json for example looks like this
[[
"Id" : "65400000-0000-0000-0000-000000000002",
"SomeProperty" : null
]]
representing an Array of Type Data containing just one Data object.
Question: Is it possible to send a request matching the aforementioned scenario ( a request containing the JSON object in the body not the actual file) from the Background, with the given fact that in iOS it's just possible to do a background upload from a file when the app is suspended after the timelimit is reached.
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?
Is it possible to read a binary encoded QR Code with AVFoundation?
I can get a AVMetadataMachineReadableCodeObject object of .type AVMetadataObjectTypeQRCode, however this only has a stringValue property, which won't work, because the data contained in the QR Code can't be converted to a string friendly representation.
Should I use ZXing instead?
Thanks.
The raw data does exist in your AVMetadataMachineReadableCodeObject, but it's not available through a public getter.
However, you can use KVO to extract it, but Apple might reject your app. Also, future iOS versions might change their private APIs and your code could become invalid (because of the hardcoded private keys).
Swift:
readableCodeObject.valueForKeyPath("_internal.basicDescriptor")!["BarcodeRawData"]
Objective-C
[readableCodeObject valueForKeyPath:#"_internal.basicDescriptor"][#"BarcodeRawData"];
I tested this for iOS 8 and 9.
I was able to solve this issue by Base64 encoding the data in the QR code.
This obviously won't work if you're not also generating the QR codes but could be option for people that are.
We were running into the upper limit of data that can be stored in a QR code but by compressing the data (we used zlib) and then Base64 encoding the compressed data, so long as your data compresses to less than 75% of its original size you get some additional capacity and can use the stringValue property to get your data back out, you just have to Base64 decode and then decompress to get the original data back.
Even if you're starting with binary data that isn't very compressible, so long as you can handle the overhead of Base64 and still be within the limitations of QR codes this may be a viable option that avoids working around the fact that AVMetadataMachineReadableCodeObject seems to want to work with string values.
You can use a CIDetector to get to a CIQRCodeFeature which has a symbolDescriptor which has a errorCorrectedPayload which contains the data.
Only problem is that this data still includes QR code headers, like ECI etc... so you still need to interpret the bits.
I summed it up in a post here.
Inspired by previous answers and other sites, I have created a gist that allows to extract binary from QR code or Aztec code, without using private APIs nor other library. It is a AVMetadataMachineReadableCodeObject extension presenting a binaryValue.
However, it only runs on iOS 11 and later, because of the CIQRCodeDescriptor use.
It is available here : https://gist.github.com/PetrusM/267e2ee8c1d8b5dca17eac085afa7d7c
For QR codes, it works only with 100% binary ones. But if they contain further parts, you can easily adapt it.
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 using rails and I would like to store a struct in my db. I figured the best way to do this would be to serialize it as json or yaml and then retrieve it, but I am running into some issues. Mostly, when I I look at the db the info looks like it's stored fine, but when I try to retrieve it, all of the info is escaped and encoded strangely.
Can someone point me in the right direction in terms of learning about json encoding and storing in a db? Also good ways of retrieving it and running the json?
Thanks so much! Let me know if you would like some examples.
Are you doing JSON.parse(#object.struct_column) while reading data back? For example:
#my_object = MyObject.find(params[:id]))
config = JSON.parse(#my_object.struct_column)
Also, these two are good reads in that connection.