41
votes

I have following struct:

public protocol SuperModel {
    // empty protocol
}
struct ModelOne: SuperModel {
    struct SubModelOne {
        var someVar: Double
        var othervar: Double?
    }
    var sub: SubModelOne?
    mutating func setSub(sub: SubModelOne) {          
        self.sub = sub
    }
}

In my class, I want to use this struct like that:

final class SomeClass: SuperClass {
    var data: SuperModel
    init() {
        self.data = ModelOne()
    }
    func someFunc() {
        (self.data as! ModelOne).setSub(ModelOne.SubModelOne(someVar: 2, otherVar: 1))
    }
}

I get following error: Cannot use mutating member on immutable value of type 'ModelOne'. Why is that so and how can I fix this?

6
What is NetworkCheckModel? A self-contained example would be useful. - Martin R
try this var data = self.data as! ModelOne data.setSub(ModelOne.SubModelOne(someVar: 2, othervar: 1)) - Sahil
@MartinR sorry I was in a rush and forget to change it. Edit was made - j0h4nn3s
@SahilBeri this works, but that means i would have a copy of it. Can I do self.data = data do copy it back? - j0h4nn3s
You can do it self.data = data but then you will not able to call setSub func using only self.data . only then you can call only those which is define in SuperModel protocol. - Sahil

6 Answers

46
votes

When you apply type casting to value types (such structs), if succeed, you receive immutable copy of requested value:

(self.data as! ModelOne) // this is copy of data

The only way (as known to me) how you can mutate values that need to be casted - reassign value (as @Sahil Beri pointed you need declare variable):

func someFunc() {
    if var data = data as? ModelOne {
        data.setSub(ModelOne.SubModelOne(someVar: 2, otherVar: 1))
        self.data = data // you can do this since ModelOne conforms to SuperModel
    }
}
1
votes

Problem is that you have declared data as SuperModel but allocate it as ModelOne. Declare data as ModelOne. Then the problem goes away.

final class SomeClass: SuperClass {
    var data: ModelOne
    init() {
        self.data = ModelOne()
    }
    func someFunc() {
        (self.data).setSub(ModelOne.SubModelOne(someVar: 2, otherVar: 1))
    }
}
1
votes

In Swift 3, in my case, I was able to resolve the error just by changing struct to a class object.

1
votes

Use like this,

struct UserAttributes {
var name:String?
var organizationID:String?
var email:String?

mutating func parseUserAttributes(attribues:[AWSCognitoIdentityProviderAttributeType])->UserAttributes{

    for type in attribues{
        if type.name == "name"{
            name = type.value
        }else if(type.name == "family_name"){
            organizationID = type.value
        }else if(type.name == "custom:role_id"){
            role = type.value
        }else if(type.name == "email"){
            email = type.value
         }

     }

   }  
 }

In some other file call like this,

var userAttributes = UserAttributes()
userAttributes = userAttributes.parseUserAttributes(attribues:attributes)
0
votes

First downcast the self.data to ModelOne then call setSub function

 if var data = self.data as? ModelOne {
   data.setSub(ModelOne.SubModelOne(someVar: 2, othervar: 1))
 }
0
votes

@Shadow of is right. You try to mutate a temporary structure which is impossible and most of the time useless as it will be released once the mutation done. It's in fact a similar issue to trying to modify the return struct of a function. (see answer here : Cannot assign to property: function call returns immutable value)