swift - Cannot assign to property: function call returns immutable value -


consider following example.

struct astruct{     var = 0 }  class aclass{     var = 0     var a: = a(i: 8)      func astruct() -> astruct{         return     } } 

if try mutate the variable of instance of class aclass compiles successfully.

var ca = aclass() ca.a.i = 7 

but if try mutate return value of astruct method, compile screams

ca.astruct().i = 8 //compile error. cannot assign property: function call returns immutable value. 

can explain this.

this compiler's way of telling modification of struct useless.

here happens: when call astruct(), copy of a passed you. copy temporary. can examine fields, or assign variable (in case able access modifications back). if compiler let make modifications temporary structure, have no way of accessing them back. why compiler programming error.


Comments

Popular posts from this blog

javascript - Laravel datatable invalid JSON response -

java - Exception in thread "main" org.springframework.context.ApplicationContextException: Unable to start embedded container; -

sql server 2008 - My Sql Code Get An Error Of Msg 245, Level 16, State 1, Line 1 Conversion failed when converting the varchar value '8:45 AM' to data type int -