Learning Kotlin and Swift

Kotlin vs Swift: The Init Construction

Explore Kotlin and Swift together and learn their differences

Elye
Elye
Oct 18, 2020 · 3 min read
Image for post
Image for post
Photo by Mason Kimbarovsky on Unsplash

In Mobile development, the 2 major programming language to learn is Swift and Kotlin. They look very similar, but there are subtle differences.

Let’s look at the one called during construction, the that exist in both Kotlin and Swift.

The init function

In Swift, is the constructor and is called before the parent is constructed.

In Kotlin, the is not really a constructor, and it is called after the parent is constructored.

More details below.

In Swift

  1. The function can have parameters to form a constructor for the class.
  2. Cannot have more than one function in the class (i.e. empty parameter constructor)
  3. The function is called before the parent constructor is called i.e. the parent object is not yet formed.
  4. The function cannot call the class member function unless it’s a static function. The reason is because the parent is not constructed yet, so the child object is not constructed yet.

The result of the above code will be

In Kotlin

  1. The scope cannot have parameters to form a constructor for the class. The is the actual constructor function that can have parameter.
  2. Can have more than one scope in the class. The execution order dependent of the order where it is placed
  3. The scope is called after the parent constructor is called i.e. the parent object is already formed. (note, the function is also called after the parent is constructed)
  4. The scope can call the class member function .The reason is because the object has been constructed.

The result of the above code will be

Summary

To summarize, I have put them side by. side for easier comparison.

Image for post
Image for post

Mobile App Development Publication

Sharing Mobile App Development and Learning

Elye

Written by

Elye

Passionate about learning, and sharing mobile development and others https://twitter.com/elye_project https://www.facebook.com/elye.proj

Mobile App Development Publication

Sharing iOS, Android and relevant Mobile App Development Technology and Learning

Elye

Written by

Elye

Passionate about learning, and sharing mobile development and others https://twitter.com/elye_project https://www.facebook.com/elye.proj

Mobile App Development Publication

Sharing iOS, Android and relevant Mobile App Development Technology and Learning

Medium is an open platform where 170 million readers come to find insightful and dynamic thinking. Here, expert and undiscovered voices alike dive into the heart of any topic and bring new ideas to the surface. Learn more

Follow the writers, publications, and topics that matter to you, and you’ll see them on your homepage and in your inbox. Explore

If you have a story to tell, knowledge to share, or a perspective to offer — welcome home. It’s easy and free to post your thinking on any topic. Write on Medium

Get the Medium app

A button that says 'Download on the App Store', and if clicked it will lead you to the iOS App store
A button that says 'Get it on, Google Play', and if clicked it will lead you to the Google Play store