Questions tagged [swift-structs]

Use this tag for questions related to Swift structs, which allows you to create a structured data type, in Swift.

A allows you to create a structured data type which provides storage of data using properties and extending its functionality via methods.

as discussed here.

You are encouraged to use the along with .

69 questions
0
votes
1 answer

Access swift structure attribute using a variable

I am new to swift and I am trying to find out, how to access the structure attribute through a variable. Being a JS developer, we are able to access the object key through a variable, so I am trying to find out if swift is also able to do the…
nahsiloh
  • 21
  • 1
  • 4
0
votes
1 answer

Update and append decoded JSON into struct?

I'm receiving some paginated JSON response through my API that looks something like this: page 1: { "count": 96, "next": "http://127.0.0.1:8000/id=5/?page=2", "previous": null, "results": [ { "id": 10, …
erikvm
  • 858
  • 10
  • 30
0
votes
1 answer

How to decode a dictionary with a nested array inside?

I'm fetching some data from an endpoint, and the response looks like this: { "count": 29772, "next": null, "previous": null, "results": [ { "id": 29, "book_name": "Book title", "book_id":…
erikvm
  • 858
  • 10
  • 30
0
votes
1 answer

Decoding two different JSON responses with one struct?

I'm receiving the same json structure from two endpoints, the only thing different are the keys in the json. On response #1 I get [ { "id": 45, "chapter__book__name": "Alonso", "chapter__book__id": 70, …
erikvm
  • 858
  • 10
  • 30
0
votes
2 answers

Swift count non empty instances in struct

I trying to count the number of non empty string in a struct to set the numberOfRow in the tableView. Some of data will return me "" when the data no need to be display in the tableView. So I need to count the numberOfRow according to the non "" in…
Aatrox11
  • 65
  • 4
0
votes
0 answers

how can I represent this json data as a String to be used in my http request

There is a block of JSON data I want to use in a http POST Request for Firebase Cloud Messaging and I will show an image of it down below: How can I convert this into something that can be encoded in Swift? I used quicktype.io to see if I can…
0
votes
1 answer

How to pass data into swiftui view and access it

Im trying to pass data into a swiftui view to be displayed when it is initialized, but am having trouble getting it to work. Here is my code which passes data into my 'MarkerInfoWindow.swift' view: func mapView(_ mapView: GMSMapView,…
zlyt
  • 259
  • 5
  • 23
0
votes
1 answer

I return a NIL using JSON Codable in Swift 4 - trying to return a value from a nested JSON example

I am trying to retrieve JSON data from a nested structure. I have shown a trimmed down version in 'jsonString' - I have copied in return results via print statements. what am I missing? all I want is the ItemCount number from this JSON data. Any…
MFK
  • 37
  • 3
0
votes
3 answers

Changing value of a struct has no effect

I have UICollectionView that have a model with following: class MainVCModel { let models = [ CellModel.init(UIImage.init(named: "1.jpg")!), CellModel.init(UIImage.init(named: "2.jpg")!), …
Evgeniy Kleban
  • 6,794
  • 13
  • 54
  • 107
0
votes
2 answers

Use Swift Decoder to pull attributes from JSON array

I have a JSON array created using this call: guard let json = (try? JSONSerialization.jsonObject(with: content, options: JSONSerialization.ReadingOptions.mutableContainers)) as? [Any] else { print("This is not JSON!!!") return } I am trying…
Jacobi
  • 21
  • 1
  • 6
0
votes
1 answer

Swift - using #available inside Struct for initializing variable

I need to initialize a variable depending on the OS version. I'm trying to use #available inside a `struct. I need to initialize a variable at the time of declaration. Here is my code that I tried, struct ColorConstants { static var os = 0 …
Pramod More
  • 1,220
  • 2
  • 22
  • 51
0
votes
1 answer

Issue with passing proper image to tableviewcell

This is my struct... struct ProductImage { let id : String let url : URL let isDefault : Bool } struct Product { let name : String let id : String var images = [ProductImage]() init(name : String, id: String) { …
user8653398
0
votes
0 answers

Why can't I initialize this struct?

I want to make a Swift Error that describes how the Type of a variable coming from Objective-C was wrong. Here's how I tried to define the struct: public struct UnexpectedResultTypeError: Error { let expected: Expected.Type …
Ky -
  • 30,724
  • 51
  • 192
  • 308
0
votes
0 answers

Building an iOS application using swift 3

I'm relatively new to iOS development but have completed some online course. I have a design question. QUESTION: I have 5 different sets of data, 2 of the data sets have the same fixed properties, the other three data sets have fixed stored…
-1
votes
1 answer

How can I make a "Dummy" struct in swift so it returns one struct or another based on a variable

How can I make a swift struct return another struct based on a switch statement? I need it to work like the so: if the time is afternoon then MainStruct will return StructA, and if the time is morning, MainStruct will return StructB. I tried…
Andrew
  • 25
  • 3