💡Xcode Tip: How to Generate Initializer for Classes
Read this tip to learn how to save time on writing initializers for classes.
Swift structs come with default memberwise initializer, i.e. the one that accepts values for all properties. Unfortunately, it is not the case for classes, so that we have to manually write boilerplate initialization code.
Xcode has lesser-known refactoring feature that generates initialization code for you: Right click class name > Refactor > Generate Memberwise Initializer.

Here is how generated code looks:
// The below class has initializer generated by Xcode
class User {
internal init(firstName: String?, lastName: String?) {
self.firstName = firstName
self.lastName = lastName
}
let firstName: String?
let lastName: String?
}
If you enjoyed this story, follow me on Twitter @V8tr and visit my blog where I discuss advanced Swift topics. Thanks!