model - How to automatically create an initializer for a Swift class? -
this model class. possible create init method automatically? everytime have initialize variables 1 one , costs lot of time.
class profile { var id: string var name: string var image: string init(id: string, name: string, image: string) { self.id = id self.name = name self.image = image } } i want self.id = id , other variables initialize automatically.
no, there no such feature classes. but, if design struct, memberwise initializer free — assuming don't define others initializers yourself.
for instance:
struct point { var x: float var y: float } ... var p = point(x: 1, y: 2) from the swift programming language book:
structure types automatically receive memberwise initializer if not define of own custom initializers. unlike default initializer, structure receives memberwise initializer if has stored properties not have default values.
the memberwise initializer shorthand way initialize member properties of new structure instances. initial values properties of new instance can passed memberwise initializer name.
Comments
Post a Comment