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
3
votes
3 answers

What is the maximum number of state variables I can have in a Swift UI Struct and will the performance slow down with increase in variables?

What is the maximum number of state variables I can have in a Swift UI Struct ? struct Example: View { @State var first : Bool = true @State var second: Double = 94.4 @State var third: CGFloat = 45.45 . . . …
Tilak Madichetti
  • 4,110
  • 5
  • 34
  • 52
3
votes
2 answers

Swift - Dynamic Array of Struct

I have a struct: public struct Deque { private var array = [T]() public var isEmpty: Bool { return array.isEmpty } public var count: Int { return array.count } public mutating func enqueue(_ element: T)…
grantespo
  • 2,233
  • 2
  • 24
  • 62
2
votes
1 answer

Trouble assigning text recognized by Swift's Vision library to the instance attribute of a struct for display

I am trying to read text from an image using Swift's Vision library. I followed this guide - https://developer.apple.com/documentation/vision/recognizing_text_in_images. With respect to my code, the image in question is self.image, which is a…
Jaiz
  • 23
  • 4
2
votes
1 answer

Why doesn't swift allow me to create a protocol type field?

As I know, structure is a value type and it stores all it's fields nearby in a continuous memory segment, the size of which should be known at the time of compilation. So, as I think, because of the constant size of the reference to some object,…
2
votes
2 answers

What is the correct way to initialize an embedded Struct?

Scenario: I have a struct within a Struct that is used to hold JSON data. Goal: I want to be be able to populate the sub struct via an init() for some UnitTesting work. struct PortEligibility: Codable { var myVar = "" struct…
Frederick C. Lee
  • 9,019
  • 17
  • 64
  • 105
2
votes
2 answers

How to update the values of a struct in a View

In SwiftUI I have a struct that want to hold data of the View. Let's say there is a View that user can create a recipe in it. It has a text field to type the recipe name, and options to choose and add to the array in the struct's properties. I…
Danny
  • 129
  • 1
  • 8
2
votes
1 answer

Swift: Cannot invoke 'decode' with an argument list of type '([Idea], from: Data)'

I've been trying to figure out the best solution for data persistence for an app I'm working on and I decided a locally stored JSON file will be the best balance of simplicity and functionality. What I need to save is an array of custom structs, and…
CristianMoisei
  • 2,071
  • 2
  • 22
  • 28
2
votes
4 answers

How to change property value with static method?

In this simple game there is a class Fighter whose purpose is to make two fighters fight. The one who looses health below 0, it looses the game. In order to fight there is a static method fight (..) which iterates till one fighter wins the game,…
Fausto Checa
  • 163
  • 13
2
votes
1 answer

Swift Struct idiom to replace OOP Type Inheritance Pattern?

Let's say I have a program that works with Rectangles (it's a simplified example of my real problem), which I model as struct Rectangle { let left:Int let right:Int let top:Int let bottom:Int func rationalized() -> Rectangle { …
Travis Griggs
  • 21,522
  • 19
  • 91
  • 167
2
votes
0 answers

Running out of memory in a for loop swift (4)

I'm looping through all pages in a PDFDocument (200+ pages) but app crashes with Message from debugger: Terminated due to memory issue The pdf is approx 4mb in size yet each iteration of the loop jumps the memory up approx 30mb. Which doesn't…
RyanTCB
  • 7,400
  • 5
  • 42
  • 62
2
votes
1 answer

swift 4 access C struct values in library

I am trying to access C values stored in the Aubio library and believe it's how I am accessing the Struct value. The library has C Struct and fvec_get_data function: typedef struct { uint_t length; /**< length of buffer */ smpl_t *data; /**<…
robmsmt
  • 1,389
  • 11
  • 19
2
votes
1 answer

Stopping reference variables changing the value of the original variable

I am assigning the value of a custom class to another variable. Updating the value of the new variable is affecting the value of the original variable. However, I need to stop the reference variable from updating the original variable. Here's a…
Micah Simmons
  • 2,078
  • 2
  • 20
  • 38
2
votes
1 answer

Searching objects created by struct

I want to search objects created by struct. Let's assume that these are our objects created by Candy struct. candies = [ Candy(category:"Chocolate", name:"Chocolate Bar"), Candy(category:"Chocolate", name:"Chocolate Chip"), …
do it better
  • 4,627
  • 6
  • 25
  • 41
2
votes
2 answers

accessing struct from one class to another

Is it possible to access a struct from another class? ex: class A{ struct structOfClassA { func returnLetterA () -> String{ return "a" } } } class B{ let classA = A() init(){ …
Encio Peter
  • 329
  • 4
  • 13
2
votes
1 answer

Swift polymorphic closure dispatch with struct/protocols

I have a case where I want to register either one argument or no argument closures with a service. There's always an argument available, but for brevity, I want to be able to register no arg closures as well, and then just dispatch the closure…
Travis Griggs
  • 21,522
  • 19
  • 91
  • 167