Protocol: Cannot assign to 'X' in 'Y' in Swift -



Protocol: Cannot assign to 'X' in 'Y' in Swift -

i defined simple protocol , a class using generics can handle protocol.

in lines marked error error: "cannot assign 'flag' in 'aobj'.

protocol flag { var flag: bool {get set} } class testflag<t: flag> { func toggle(aobj: t) { if aobj.flag { aobj.flag = false; // <--- error } else { aobj.flag = true; // <--- error } } }

do have thought why , have alter prepare it?

from docs:

function parameters constants default. trying alter value of function parameter within body of function results in compile-time error. means can’t alter value of parameter mistake.

in case, can add together inout toggle persisted beyond function call:

func toggle(inout aobj: t) { if aobj.flag { aobj.flag = false; else { aobj.flag = true; } }

you have done:

func toggle(var aobj: t) { }

but might not accomplish wanted.

swift protocols

Comments

Popular posts from this blog

php - Android app custom user registration and login with cookie using facebook sdk -

django - Access session in user model .save() -

php - .htaccess Multiple Rewrite Rules / Prioritizing -