💡Xcode Tip: How to Generate Initializer for Classes

Vadim Bulavin
1 min readAug 1, 2019

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!

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

Vadim Bulavin
Vadim Bulavin

Written by Vadim Bulavin

Senior iOS Software Engineer at Pluto TV. Creator of Yet Another Swift Blog https://www.vadimbulavin.com/. Coding for fun since 2008, for food since 2012.

Responses (1)

Write a response

any different between internal init() and init() ?

--