swift-structs

How to update the values of a struct in a View

萝らか妹 提交于 2020-06-01 05:36:47
问题 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 managed to make the struct and introduce it in the View but I cannot change it values. The values of the struct should be updated based on what users does on the View and the info they add to it. I made a simple View that whatever is entered in the

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

怎甘沉沦 提交于 2019-12-12 11:34:41
问题 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 mutate it as so: var p = Point() p.add(1) Now does the existing struct in memory get mutated, or is self replaced with a new instance as in self = Point(x:self.x+1) 回答1: Now does the existing struct in memory get mutated, or is self replaced with a new instance

Issue with passing proper image to tableviewcell

…衆ロ難τιáo~ 提交于 2019-12-11 14:15:03
问题 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) { self.name = name self.id = id } mutating func add(image: ProductImage) { images.append(image) } } Now I have an image loaded on the collectionview and on the click of a button, I want to pass this image to a tableviewcell . The collectionview does have a couple of labels with name and

Swift structures: handling multiple types for a single property

拟墨画扇 提交于 2019-12-03 19:36:28
问题 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 I cannot figure out how to handle this case because struct ItemRaw: Codable { let parentType: String enum CodingKeys: String, CodingKey { case parentType = "type" } } throws "Expected to decode String but found a number instead." , and naturally, struct

Swift mutable structs in closure of class and struct behave differently

假如想象 提交于 2019-12-03 07:01:29
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 in turns calls a function of some class(B). This class's function again takes a closure. In this closure's body mutate the struct, i.e. change the name property, and call the closure that was supplied by the first class. When the first class (A) closure is called where we are inspecting the struct's name property, it is never changed. But in step 2

accessing struct from one class to another

旧街凉风 提交于 2019-12-02 04:01:42
问题 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(){ classA.structOfClassA.returnLetterA // this is what I want to achieve } } how can I access the the struct from Class A() in Class B()? is there a workaround with this? Thank you! 回答1: The structure in class A defines a type (that can be used within the scope of class A ), but you need an instance of it to be able to call

Swift structures: handling multiple types for a single property

痞子三分冷 提交于 2019-11-30 09:53:06
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 I cannot figure out how to handle this case because struct ItemRaw: Codable { let parentType: String enum CodingKeys: String, CodingKey { case parentType = "type" } } throws "Expected to decode String but found a number instead." , and naturally, struct ItemRaw: Codable { let parentType: Float enum CodingKeys: String, CodingKey { case parentType = "type" } }

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

China☆狼群 提交于 2019-11-26 10:57:55
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 of type '[SomeStruct]' to type '[Any]' I've been trying to find the logic behind this, but with no luck. It's worth mentioning if you change the struct to a class, it works perfectly. One could always add a workaround and map each object of the fooArray and cast them to type of Any, but that is not the issue here. I'm looking for an explanation on why

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

假如想象 提交于 2019-11-26 02:15:30
问题 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 of type \'[SomeStruct]\' to type \'[Any]\' I\'ve been trying to find the logic behind this, but with no luck. It\'s worth mentioning if you change the struct to a class, it works perfectly. One could always add a workaround and map each object of the