Swift in 24 Hours, Sams Teach Yourself by BJ Miller

Swift in 24 Hours, Sams Teach Yourself by BJ Miller

Author:BJ Miller [Miller, BJ]
Language: eng
Format: azw3
Publisher: Pearson Education
Published: 2015-10-29T04:00:00+00:00


Q&A

Q. Can I have default values assigned where I declare my properties and still use an initializer to set those properties?

A. You certainly can. As long as each stored property has been assigned a value when class initialization is finished, you won’t get an error.

Q. Can a class have more than one designated initializer?

A. Yes, as long as the method signatures are different, a class can have as many designated initializers as you want. Because subclasses can only call designated initializers in superclasses, this can be useful when you want subclasses to have more than one way to initialize your class.

Q. If I’m creating a subclass, when should I call super.init() in my initializer?

A. You can only call an initializer in a superclass after you have fully initialized your own stored properties. This is to prevent your superclass from accidentally trying to access a property that has not been initialized and thereby causing an error. Consider the case in which you have overridden a method so that it changes the value of a stored property introduced in your subclass. If super.init() calls this overridden method, it invokes the subclass’s version instead of its own and tries to set a property that hasn’t been initialized yet, which would cause an error.

Q. Are initializers inherited from a parent class?

A. If you provide default values for every stored property in your class and provide no initializers, your class will inherit its superclass’s initializers. Otherwise, you will not inherit any initializers and you will have to provide your own.



Download



Copyright Disclaimer:
This site does not store any files on its server. We only index and link to content provided by other sites. Please contact the content providers to delete copyright contents if any and email us, we'll remove relevant links or contents immediately.