Cannot Assign Property in Method of Struct

Cannot assign property in method of struct

If you want to modify the properties of the struct, mark the function as mutating.

struct Foo {
var bar = 1

mutating func baz() {
bar = 2
}
}

Cannot assign property of struct returned from method

I now solved it by returning a CellCollection from the Cells property which contains an indexer.
This way Iam no more getting the error and I just create one single object for all cells.

Cannot assign to property: 'self' is immutable, I know how to fix but needs understanding

struct is a value type. For value types, only methods explicitly marked as mutating can modify the properties of self, so this is not possible within a computed property.

If you change struct to be a class then your code compiles without problems.

Structs are value types which means they are copied when they are passed around.So if you change a copy you are changing only that copy, not the original and not any other copies which might be around.If your struct is immutable then all automatic copies resulting from being passed by value will be the same.If you want to change it you have to consciously do it by creating a new instance of the struct with the modified data. (not a copy)

Cannot assign to property: '$0' is immutable

Replace this:

        languageTitles.forEach { $0.isSelected = false  }

With:

for index in languageTitles.indices {
languageTitles[index].isSelected = false
}

forEach has various subtle and surprising behaviors. It is generally best to use a for-in instead. But the key issue is that LanguageModel is a struct and has value semantics. When you assign it to a variable (i.e. $0, or the variable in a for-in loop), a copy is made. If you make changes to the copy, they don't modify the array. (By default they're constants, so you can't modify them anyway.) Assigning through the subscript operator, however, does what you expect.

how to assign value to a get-only property

You have two problems.

First, as you have discovered, you can't modify vote because it is a computed property. Its value is determined by the code in its getter method. Assigning a value doesn't make sense.

I guess you have used a computed property because your JSON source is, frankly, awful. Integers should be sent as integers, not strings.

Assuming you can't change the JSON to something more sensible, then you will just need to update the underlying DOUBAN_VOTES property in your upVote function. You will need to make DOUBAN_VOTES a var, not a let.

This will reveal your second problem; Structs are immutable. In order to allow a function to change a value, it must be flagged as a mutating function.


var DOUBAN_VOTES:String

mutating func upVote(newVote: Int) {
self.DOUBAN_VOTES = "\(newVote)"
}

Cannot assign to property: 'desc' is a 'let' constant

    class infoViewController: UIViewController {

var result:Data1!

override func viewDidLoad() {
saveToJsonFile(result2 : &result!)
}

func saveToJsonFile(result2 : inout Data1) {
result2.data[1].monday[1].desc = "cos"
}
}

struct Data1{
var data: [Monday]
}
struct Monday {
var monday: [Desc]
}
struct Desc{
let desc: String
}

If you try as above you will get "Cannot assign to property: 'desc' is a 'let' constant" error.
So you need to change the let into var, because let is immutable.

class infoViewController: UIViewController {

var result:Data1!

override func viewDidLoad() {
saveToJsonFile(result2 : &result!)
}

func saveToJsonFile(result2 : inout Data1) {
result2.data[1].monday[1].desc = "cos"
}
}


struct Data1{
var data: [Monday]
}
struct Monday {
var monday: [Desc]

}
struct Desc{
var desc: String
}


Related Topics



Leave a reply



Submit