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
39
votes
2 answers

swift - can I call a struct default memberwise init from my custom init method?

If I create a swift struct with no init, then I can call the compiler-generated default memberwise initialiser, like so: struct OrderFill { let price:Int let qty: Int let timeStamp: NSDate } let o = OrderFill(price: 2, qty: 1, timeStamp:…
Orion Edwards
  • 121,657
  • 64
  • 239
  • 328
18
votes
4 answers

Swift structures: handling multiple types for a single property

I am using Swift 4 and trying to parse some JSON data which apparently in some cases can have different type values for the same key, e.g.: { "type": 0.0 } and { "type": "12.44591406" } I am actually stuck with defining my struct because…
errata
  • 5,695
  • 10
  • 54
  • 99
16
votes
2 answers

Why isn't [SomeStruct] convertible to [Any]?

Consider the following: struct SomeStruct {} var foo: Any! let bar: SomeStruct = SomeStruct() foo = bar // Compiles as expected var fooArray: [Any] = [] let barArray: [SomeStruct] = [] fooArray = barArray // Does not compile; Cannot assign value…
Laffen
  • 2,753
  • 17
  • 29
13
votes
3 answers

Does a mutating struct function in swift create a new copy of self?

I like value semantics in swift but I am worried about the performance of mutating functions. Suppose we have the following struct struct Point { var x = 0.0 mutating func add(_ t:Double){ x += t } } Now suppose we create a Point and…
gloo
  • 2,490
  • 3
  • 22
  • 38
12
votes
3 answers

Swift mutable structs in closure of class and struct behave differently

I have a class(A) that has a struct variable (S). In one function of this class I call a mutating function on struct variable,this function takes a closure. Body of this closure checks the struct variable's name property. Struct's mutating function…
tarun_sharma
  • 761
  • 5
  • 22
11
votes
2 answers

Swift 4.2) Mutate array of struct with for_in/forEach vs. access by index

I am trying to modify struct element in array. I found that you can do that by accessing(iterate) the struct by index, but you can't if you use 'for in' loop or forEach{}. struct Person { var age = 0 var name = "James" } var personArray =…
juliannehong
  • 111
  • 1
  • 3
10
votes
0 answers

Swift 2.2 MVVM view Model as mutable struct doesn't maintain state in view controller

I am fairly aware about difference between a value type and reference type in Swift and aware that value types are designed to be immutable usages. But structs in particular have ability to mutate themselves and that is my concern. How to…
tarun_sharma
  • 761
  • 5
  • 22
9
votes
1 answer

Atomic property wrapper only works when declared as class, not struct

I have created a "lock" in Swift and an Atomic property wrapper that uses that lock, for my Swift classes as Swift lacks ObjC's atomic property attribute. When I run my tests with thread sanitizer enabled, It always captures a data race on a…
7
votes
2 answers

How can I easily copy an immutable struct in swift, varying only some fields [Like kotlin dataclass copy method]?

I have a swift struct somewhat like this: struct LogicalState { let a: String? let b: Bool let c: Int } and a mutable instance of this state. Note the properties within the state are all let, so the struct itself is immutable. var _state:…
Orion Edwards
  • 121,657
  • 64
  • 239
  • 328
7
votes
5 answers

Why doesn't didSet work?

In many examples of didSet I see on SO, this code will return 0, however, I can't get it to return anything other than the original value. What am I doing wrong? Swift struct Circle { var radius: Double { didSet { if radius <…
nipponese
  • 2,813
  • 6
  • 35
  • 51
5
votes
1 answer

How to properly decode nested JSON objects with Swift structs

Intent: Receive cryptocurrency price data via Coinmarketcap API, decode it into custom structs in SWIFT and potentially store that data in a database (either CoreData or SQLite). Context: I am receiving the following error on…
K307
  • 105
  • 7
5
votes
3 answers

Swift memoizing/caching lazy variable in a struct

I drank the struct/value koolaid in Swift. And now I have an interesting problem I don't know how to solve. I have a struct which is a container, e.g. struct Foo { var bars:[Bar] } As I make edits to this, I create copies so that I can keep an…
Travis Griggs
  • 21,522
  • 19
  • 91
  • 167
5
votes
4 answers

Is there way to define compare (`==`) function automatically for `struct` in Swift?

Let's assume we have a pretty big struct in Swift: struct SuperStruct { var field1: Int = 0 var field2: String = "" // lots of lines... var field512: Float = 0.0 } .. and then we need to implement Equatable protocol: extension…
Valentin Shergin
  • 7,166
  • 2
  • 50
  • 53
4
votes
1 answer

What copy-elision does swift actually do for structs?

The general consensus for Swift programming (as at May 2018, Swift 4.1, Xcode 9.3) is that structs should be preferred unless your logic explicitly calls for a shared reference to an object. As we know, a problem with structs is that they're passed…
Orion Edwards
  • 121,657
  • 64
  • 239
  • 328
4
votes
2 answers

How to change the value of a child from a Mirror introspection

I'm doing a bunch of BLE in iOS, which means lots of tight packed C structures being encoded/decoded as byte packets. The following playground snippets illustrate what I'm trying to do generically. import Foundation // THE PROBLEM struct Thing { …
Travis Griggs
  • 21,522
  • 19
  • 91
  • 167
1
2 3 4 5