693 questions
2
votes
1
answer
223
views
Swift 6 concurrency - how to make a UIView Codable?
How do you make a UIView Codable under Swift 6 concurrency?
To see the problem, start with this:
final class MyView: UIView {
var name: String
init(name: String) {
self.name = name
...
0
votes
0
answers
100
views
Is it possible to have non-decoded values in a Decodable struct?
I have a "Course" struct that is used in decoding certain JSON values:
struct Course: Decodable, Identifiable {
var name: String?
var courseCode: String
var id: Int
var ...
2
votes
4
answers
169
views
Decoding a Swift type which is only conditionally `Codable`
I have a Swift class that may or may not be Codable. When it is Codable, I would like to be able to load it from file. Here is a bare bones example...
protocol P {
func load()
}
final class A<...
0
votes
0
answers
68
views
Codable class for nested dictionary in Swift
I am currently trying to parse a nested dictionary response using Codable and trying to not use too much custom decoders.
JSON response looks similar to below:
{
"id": "1234",
&...
0
votes
0
answers
54
views
Swift Json Decoder how to get a nested part of the object without decoding it
I receive server response which contains geojson I need to have the geojson as string and don't want to decode it, but this code throws type mismatch exception.
init(from decoder: Decoder) throws {
...
1
vote
1
answer
41
views
Getting errors while using decodable extension in different module in Swift
I am facing issues while using init(from decoder: Decoder) in Model extension in different module.
I have model in Module 1 as below
public struct LabelModel {
public var value: String?
}
And I ...
2
votes
4
answers
134
views
How to parse JSON that is not defined in CodingKeys
Consider the following JSON:
{
"jsonName": "fluffy",
"color1": "Blue",
"color2": "Red",
"color3": "Green",
&...
3
votes
1
answer
120
views
How to override the default implementation of UnkeyedDecodingContainer protocol functions?
extension KeyedDecodingContainer {
func decode(_: Money.Type, forKey key: Key) throws -> Money {
let str = try decode(String.self, forKey: key)
return try str.toMoney(on: key)
...
0
votes
1
answer
44
views
How can I make Swift CodingKeys for JSON data with non-unique keys?
I am using the Unsplash API and it gives this response (which is just a snapshot)
"exif": {
"name": "Canon, EOS 6D"
},
"location": {
...
1
vote
1
answer
56
views
JSON array to properties
I'm trying to map the following JSON
{
"items": [
{
"id": 4,
"name": "Caffè",
"is_active": true,
...
0
votes
1
answer
138
views
Codable: Decode a String into a custom type (ISO 8601 Date, no time components)
Consider the following type:
public struct DocumentDate: Codable {
/// Year component of the date, Integer with no limitation on the range.
public let year: Int
/// Month component of the ...
3
votes
2
answers
193
views
Swift decoding error types inconsistency with `Bool` type
import Foundation
let json = """
{
"property": null
}
""".data(using: .utf8)!
struct Program<T: Decodable>: Decodable {
let property: T
...
-1
votes
1
answer
65
views
Type 'Self' does not conform to protocol 'Decodable' or ‘DefaultsKeyedArchiverBridge' requires that 'Self' conform to 'Decodable'
I'm encountering an issue while working on my Swift project that involves using the SwiftyUserDefaults library. I'm relatively new to Swift development and would appreciate some guidance on resolving ...
1
vote
1
answer
164
views
How to loop through json using CodingKeys and a custom decoder to build an array to assign to a struct property? [duplicate]
I have a JSON object which I want to decode into a struct. The JSON has similar key:value pairs for ingredients and measurements. I'd like to condense the ingredient and measurement information into ...
0
votes
0
answers
88
views
Cannot convert value of type StructName<X> to expected argument type 'X'
I am passing a generic Decodable struct with parameter. Here is the struct code ..
struct NameStruct<T: Decodable>: Decodable {
// properties of the struct with property..
let values:...
0
votes
1
answer
51
views
Facing difficulties to define a type for a specific key while decoding JSON
I have come across a key type in the JSON added bellow,
{
"id": "B2CAA3C8-077B-4A49-B5BA-206709630138",
"markAsDoneDate": null,
"endDate": 735025126....
0
votes
0
answers
61
views
How to decode variable object in variable array with jsonDecoder in swift
I am new in SWIFT. I have been fighting with swift for a long time to decode my custom JSON.
But I couldn't.
This is my JSON :
tablesArray is an array and it has more than one tables as jsonobject
But ...
0
votes
0
answers
58
views
Inputted Values for SignUpView not saving to Backend API
I am currently refactoring authentication for an iOS app in SwiftUI and I am having trouble getting the data associated with a new user saved to the backend API. Essentially when the user signs up, ...
1
vote
0
answers
43
views
Swift JSON string data decoding with escaping character [duplicate]
I am getting an error when I am going to decode data response which contains escaping character string .
To demonstrate, I created a playground.
import Foundation
struct Example: Decodable {
let ...
2
votes
1
answer
164
views
What is wrong with my enum decoding in Swift?
I just tried this:
let test = "{ \"de\": \"Bisasam\", \"en\": \"Bulbasaur\" }"
let data = test.data(using: .utf8)!
do {
let result = try ...
0
votes
1
answer
207
views
Use Location wrapper to decode CLLocation variable
I am currently trying to make an extension to render CLLocation codable. To do so, I am trying to replicate the example on this thread: Swift - Codable Decode array of arrays of CLLocation
I have the ...
0
votes
1
answer
434
views
How to use ticketmaster API in swift
This is the first time I have tried to use an API, I get the error "keyNotFound" Could someone point me in the right direction for understanding this better? Here is a link to their ...
0
votes
2
answers
101
views
How to encode a dictionary of unknown/variable keys?
Say I have to send this data to the server:
struct Event: Codable {
let title: String
let params: [String:Any]? //Not allowed
}
So for instance, these events could look like any of these:
let ...
0
votes
4
answers
151
views
Swift Decodable, is it possible to use JSONDecoder to parse JSON like this into my target object?
I have a WebAPI which returns JSON like the following:
{"result":"true","message":"2023-07-01 16:09:02"}
I have a Decodable object defined like this:
struct ...
0
votes
2
answers
415
views
Struct with protocol as property type does not conform to protocols 'Decodable' and 'Encodable' [duplicate]
I have a struct UISectionModel with only one property section, which has a protocol UISectionProtocol for a type, as follows:
struct UISectionModel: Codable {
let section: UISectionProtocol
}
...
0
votes
1
answer
303
views
Decode Dynamic Types in Swift
I have an API response that is returning a list of objects that are sometimes different and I am having issues trying to decode the list properly. The list will have the object type as the key and ...
0
votes
2
answers
141
views
How to set a variable to multiple types in data model
My app gets its data from an API endpoint.
The data is a Review and the model looks like this:
struct Review: Identifiable, Decodable {
var id: Int
var reviewable_type: String
var score: ...
2
votes
2
answers
129
views
Is it possible to leverage Codable to initialize a conforming type from a Dictionary
Suppose I have the following struct definition and dictionary:
struct Point: Codable {
let x: Int
let y: Int
}
let pointDictionary = [ "x": 0, "y": 1]
// Inital example ...
0
votes
1
answer
570
views
Json Parsing in Swift when a key value is getting from the json itself
I have a json like below need to parse , I have looked into many solutions . Here I need to use the value of the start key to fetch next value .
"journey": {
"start"...
-1
votes
1
answer
74
views
What are structs should be according to API docs to parse data from response?
I need structs to decode surface wind data.
https://api.windy.com/point-forecast/docs
The body of response from API docs:
{
ts:int[],
units: {
{parameter-level}: string,
{...
2
votes
1
answer
455
views
swift enum is ignoring CodingKeys
I have a some nested swift classes that are automatically decoded from JSON using the Decodable protocol. Everything works except this one enum.
The json data is a capitalised string of either Open or ...
1
vote
1
answer
680
views
Parse decodable struct property as any data type
The two structs that I am using are Scoreboard and ResultSet
struct ResultSet: Codable {
var name: String
var headers: [String]
//var rowSet: [String]
}
struct Scoreboard: Codable {
...
0
votes
2
answers
2k
views
Instance method requires that 'classname' conform to 'Decodable'
I'm trying to decode a JSON response as a custom type, that I believe conforms to Decodable.
These are the codable structs that I am using
struct ResultSet: Codable {
var name: String
var ...
1
vote
1
answer
428
views
Parse JSON as array of objects instead of dictionary using Codable in Swift
I receive the following JSON from API
{
"someProperty":"someValue",
"type":"type1",
"values":{
"first":"someValue",
...
0
votes
1
answer
526
views
Decode json with key and Any type Value
I have a JSON file and I want to use [String: Any]. The problem I'm having is that I get an error:
No exact matches in call to instance method 'decode'
I would love to understand where I am wrong.
...
0
votes
1
answer
199
views
Clarifications on JSONDecoder when decoding a single value
I was trying to perform some tests on JSONDecoder and I've encountered a strange behavior. In particular, when I use the following code an error is thrown.
let data = "Sample String".data(...
-1
votes
2
answers
103
views
Does Swift task run first or print() first when I tap my UIButton?
I am trying to understand what is going on in my code here.
I have a simple API call to open weahter API and that whenever the user taps the UIButton, it should call the api and get the data back from ...
0
votes
2
answers
705
views
Swift Codable with Custom JSONDecoder with Custom dateDecodingStrategy failing when Device is not using 24 hours format
I am getting dates from server in below format
"endTime": "2022-12-12T16:20:00.000Z"
I am using Codable to parse json to Objects. I am using a custom decoder shown below
internal ...
0
votes
0
answers
58
views
How do I access data in a nested json in Swift(while using optionals in my data model) [duplicate]
I have my struct as:
struct CategoryViewModel2: Codable {
let data: [Datum2]?
}
// MARK: - Datum
struct Datum2: Codable {
let the0: Int?
let the1, the2: String?
let the3, the4, id: ...
0
votes
1
answer
706
views
How to decode a non-UTF8 encoded JSON array using Swift?
I'm encountering some weird edge cases when parsing JSON data from the Google search autocomplete API. This is my model for decoding the JSON data:
struct suggestOutputModel: Decodable {
let query:...
0
votes
1
answer
53
views
Type 'HeroStruct.Type' cannot conform to 'Decodable', how can i solve that?
import UIKit
class ViewController: UIViewController, UITableViewDelegate, UITableViewDataSource {
var hero = [HeroStruct]()
@IBOutlet weak var tableView: UITableView!
override func ...
-1
votes
1
answer
691
views
How to Parse Nested part of a JSON based on a condition in Swift
Other suggested solutions handle type of structure they are part of, While I am not able to figure out how to parse nested part of that same structure based on the value within outer structure.
I have ...
0
votes
0
answers
147
views
How to decode JSON that is an array of arrays (of multiple types) in Swift with AlamoFire
I am trying to get JSON from an external source, using AlamoFire tools. Using the .responseJSON method works, but that will be deprecated and in any case I know .responseDecodable is more sound. ...
2
votes
1
answer
1k
views
Type cannot conform to Decodable but it actually is
I have a Type called ObjectAPIModel that explicitly conforms to the Codable protocol. I also do have a function which receives some data and calls another function to decode any JSON to a specified ...
1
vote
3
answers
745
views
Swift Decodable - Is it possible to read part of a tree as a string rather than a data structure?
Here's my problem. Let's say I have a JSON structure that I'm reading using Swift's Codable API. What I want to do is not decode part of the JSON but read it as a string even though it's valid JSON.
...
0
votes
0
answers
38
views
ignoe/bypass the key if value is nil after decoder in swift [duplicate]
My process is I have bunch of json string that I need to convert into request payload and make a POST request to api call. The issue is key may or may not exist from my original json string and the ...
0
votes
1
answer
177
views
Swift Decodable: Inject value in nested generic property
I have this API response structure (from Strapi v4):
{
"data": [
{
"id": 1,
"attributes": {
"description": &...
0
votes
1
answer
558
views
How to create decodable of nested array in swift?
Background: I'm using swift [email protected] and I need to parse json data
AF.request("https://xxx.json").validate().responseDecodable(of: Decodable)
The data looks like:
[
[
"...
0
votes
1
answer
254
views
Decodable with extra property
I have a Decodable to which I want to add one extra property, not present in the plist.
The code below seems to work. I'm not sure why, since it no longer conforms to the plist. Can anyone explain?
Is ...
0
votes
2
answers
621
views
Swift URLSession and Combine json array decode fails
Well guys like the title says i have on a response service call an JSON array and i cant find the way to decode with Combine:
URLSession.shared.dataTaskPublisher
Service Response: https://codebeautify....