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
1
vote
2 answers

How to create a JSON that conforms to this Swift Struct

I'm really sorry if this is a bit of a dumb question but I was wondering how I could create a JSON and JSON Decoder that works with the Swift Struct below. Thanks! struct Example: Hashable, Codable, Identifiable { var id: Int var title:…
1
vote
1 answer

Do something as soon as UIViewRepresentable is instantiated

This seems like a straightforward question, but I have not been able to find any solutions in the Swift docs. When I was programming in Kotlin, it was common to use the init() method of a class to perform some work in order to get the class set up…
John Harrington
  • 1,314
  • 12
  • 36
1
vote
1 answer

Use Struct as a parameter in a function in Swift

I have the next scenario : I have created a Swift package that I am using in a main application. In this Swift Package I want to use some colors because it's an UI package. My struct Colors is already defined in my main application and I don't want…
poPaTheGuru
  • 1,009
  • 1
  • 13
  • 35
1
vote
0 answers

Converting CSV file to array consumes massive amounts of memory

I have some medium large CSV files (about 140mb) and I'm trying to turn them into an array of structs. I don't want to to load the hole file in the memory so I'm using a steam reader. For each line I read the data, turn the line into my struct and…
DoTryCatch
  • 1,052
  • 6
  • 17
1
vote
1 answer

Using multiple files in SwiftUI playground

I am trying to use multiple files in SwiftUI playground. I added some code in a separate file in sources. I just want the sheet view to appear when the button is tapped. Even though I have made the struct public but I still get the error as "…
1
vote
1 answer

In Swift, how can you call a function immediately after object creation

I have some objects, which are structs, that I initialize from JSON dictionaries ([String : Any]) via an init function provided from an extension on the Decodable protocol (see Init an object conforming to Codable with a dictionary/array). So…
Tim Fuqua
  • 1,635
  • 1
  • 16
  • 25
1
vote
0 answers

Swift - structures and AnyObject

I thought that structs were not AnyObject. For instance: https://stackoverflow.com/a/33921992/2054629 But then I tried (in swift 3, xcode 8.2.1): struct Struct{ let foo = 1 } let s = Struct() s is AnyObject // true, and I get a warning saying…
Guig
  • 9,891
  • 7
  • 64
  • 126
1
vote
1 answer

Var set to struct really replaced when modified?

Happy Holidays! I got stymied on p.147 of Neuberg's "iOS 10 Programming Fundamentals". His claim is that "when you apparently mutate an instance of a value type, you are actually replacing that instance with a different instance." Question: If…
JackyChan
  • 13
  • 4
1
vote
1 answer

Access value outside of a struct in Swift 3

I'm trying to implement the PagingMenuController for my iOS app Swift 3. See the full code example here on github. In this example, they create the viewControllers inside the struct PagingMenuOptions (let viewController1 = ViewController1() ...).…
Philiz
  • 429
  • 5
  • 25
0
votes
0 answers

Decoding JSON with SWIFT CodingKeys Error

I have the following data model which I am trying to decode JSON from an API into. struct Data: Identifiable, Codable { var id = UUID() let count: Count let risk: Risk let updatedAt: String enum CodingKeys: String,…
ArQangel
  • 65
  • 5
0
votes
0 answers

Mutate Model Struct in a ForEach - Swift/SwiftUI

I have a Struct model with 3 properties: Name Date Cost I get a he first 2 properties data from a network request and I populate an array. Within that same network request in order to get the 3rd properties data I have to make a separate network…
0
votes
0 answers

Saving a custom identifiable struct in User Defaults SwiftUI

I am trying to save a simple struct in UserDefaults, but I get an error: Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'Attempt to insert non-property list object... Here is the struct: struct saveStruct :…
0
votes
1 answer

How to add network fetch inside swift struct initializer

I'm trying to get data from an URL (1st network call), then make a 2nd network call to fetch image from the url in the first response, and present on tableview in swift. The json from 1st network call contains 3 strings: struct Item: Codable { …
RobotX
  • 208
  • 2
  • 15
0
votes
2 answers

How to convert swift-struct object name using swift-string

I have a struct name called Car. Car has two attributes(noOfTyres, ownerName). struct Car { var noOfTyres: Int var ownerName: String } The string value is let objStr = "Car/ownerName" how to convert the objStr to swiftObject like…
S Surya
  • 87
  • 1
  • 1
  • 8
0
votes
1 answer

Add ID Property on Struct Fetched From JSON

I have a 'Recipe' struct whose instances are created by parsing a JSON file. All the properties of Recipe map to the JSON, except for one: id. I want each struct instance to be uniquely identifiable via this property, and it should be generated…
Sam
  • 1,130
  • 12
  • 36