As for struct , if you want to change the self property inside the computed properties, you must explicitly declare getter as mutating get { .. }
public struct PhotoStruct { var _photo:UIImage? var urlString:String? init(image:UIImage?, url:String?){ self._photo = image self.urlString = url } init(url:String?){ self.urlString = url } var photo1:UIImage? { mutating get {
Of course, the structure itself must be volatile:
var pVar:PhotoStruct = PhotoStruct(image: nil, url: nil) pVar.photo1 // no problem let pLet:PhotoStruct = PhotoStruct(image: nil, url: nil) pLet.photo1 // < [!] error: immutable value of type 'PhotoStruct' only has mutating members named 'photo1'
One warning :
As far as I know, the mutating get { } function is mutating get { } documented in the language help system .
source share