initialization - Initialize property of type immutable dictionary in Swift init() -
i initialize immutable dictionary calculating values in init()
. have:
class myclass { var images: [string: uiimage] func init() { images = [:] // ... loop on strings // ... calculate image images[string] = image } }
is there way define property let
instead of var
semantics. convenient , seem appropriate content won't changed after object has been initialized. (if change var
let
receive error assignment inside loop: "immutable value 'self.images' may not assigned to".)
make additional variable in init
, assign once images
:
class myclass { let images: [string: uiimage] func init() { var tempimages = [string: uiimage]() // ... loop on strings // ... calculate image tempimages[string] = image // assign tempimages images images = tempimages } }
Comments
Post a Comment