Statistics
55
Views
0
Downloads
0
Donations
Uploader

高宏飞

Shared on 2025-12-02
Support
Share

Authorrina Galata, Victoria Gonda, Joe Howard & Ellen Shapiro

No description

Tags
No tags
Publisher: Razeware LLC.
Language: 英文
File Format: PDF
File Size: 18.8 MB
Support Statistics
¥.00 · 0times
Text Preview (First 20 pages)
Registered users can read the full content for free

Register as a Gaohf Library member to read the complete e-book online for free and enjoy a better reading experience.

(This page has no text content)
Kotlin Apprentice By Irina Galata, Victoria Gonda, Joe Howard & Ellen Shapiro Copyright ©2021 Razeware LLC. Notice of Rights All rights reserved. No part of this book or corresponding materials (such as text, images, or source code) may be reproduced or distributed by any means without prior written permission of the copyright owner. Notice of Liability This book and all corresponding materials (such as source code) are provided on an “as is” basis, without warranty of any kind, express of implied, including but not limited to the warranties of merchantability, fitness for a particular purpose, and noninfringement. In no event shall the authors or copyright holders be liable for any claim, damages or other liability, whether in action of contract, tort or otherwise, arising from, out of or in connection with the software or the use of other dealing in the software. Trademarks All trademarks and registered trademarks appearing in this book are the property of their own respective owners. Kotlin Apprentice raywenderlich.com 2
About the Team Irina Galata is an author of this book. Irina is a software engineer in Linz, Austria, working at Runtastic. She is passionate about programming and learning new things. You can follow her on Twitter @igalata13 . Ellen Shapiro is an author of this book. She works for Apollo GraphQL, caring for and feeding their iOS SDK. She’s been building iOS and Android apps since late 2010, and has written and edited tutorials and books about iOS and Android for raywenderlich.com since 2013. She’s also developed several independent applications through her personal company, Designated Nerd Software. When she’s not writing code, she’s usually tweeting about it at @DesignatedNerd. Victoria Gonda is the author of this book. She is a software engineer who primarily works with Android. After having studied both dance production and computer science, she focused her career on software because of its potential to help people in a meaningful way. Victoria regularly speaks at conferences, which have been an enjoyable way for her to connect with the technical community and exchange ideas. In her spare time, Victoria enjoys playing board games and curling up with a good book. You can connect with her on Twitter at @TTGonda. Kotlin Apprentice raywenderlich.com 3
Acknowledgements We’d also like to acknowledge the efforts of the authors of the Swift Apprentice and previous editions of Kotlin Apprentice whose work formed the basis for parts of this book: • Janie Clayton is an independent iOS developer. She spent a year apprenticed to a super genius programming robots and learning the forgotten ways of long term software maintenance and development. Janie is the coauthor on several books on iOS and Swift development. Janie lives outside of Madison, Wisconsin with her attempted grumble of pugs and multitude of programming books. Janie writes her various musings on her blog at RedQueenCoder.com. • Alexis Gallagher is a software engineer who is always looking for the conceptual deep dive and always hoping to find pearls down at the bottom. When he’s not coding, he’s out and about in sunny San Francisco. • Matt Galloway is a software engineer with a passion for excellence. He stumbled into iOS programming when it first was a thing, and has never looked back. When not coding, he likes to brew his own beer. • Eli Ganim is an engineering manager at Facebook. He is passionate about teaching, writing, and sharing his knowledge with others. • Joe Howard is a computational physicist who studied particle physics using parallel Fortran simulations. He gradually shifted into systems engineering and ultimately software engineering around the time of the release of the iOS and Android SDKs. He’s been a mobile software developer on iOS and Android since 2009, working primarily at two agencies in Boston, MA since 2011. • Erik Kerber is a software developer in Minneapolis, MN, and the lead iOS developer for the Target app. He does his best to balance a life behind the keyboard with cycling, hiking, scuba diving, and traveling. • Ben Morrow delights in discovering the unspoken nature of the world. He’ll tell you the surprising bits while on a walk. He produces beauty by drawing out the raw wisdom that exists within each of us. • Cosmin Pupăză is a software developer and tutorial writer from Romania. He has worked with more than a dozen programming languages over the years, but none of them has made such a great impact on himself as the advent of Swift. When not coding, he either plays the guitar or studies WWII history. Cosmin blogs about Swift at cosminpupaza.wordpress.com. Kotlin Apprentice raywenderlich.com 4
• Steven Van Impe is a computer science lecturer at the University College of Ghent, Belgium. When he’s not teaching, Steven can be found on his bike, rattling over cobblestones and sweating up hills, or relaxing around the table, enjoying board games with friends. You can find Steven on Twitter as @svanimpe. • Dick Lucas is a developer by trade but adds value anyway he can. He is also a writer, podcast host, and advisor at nogradient.com. He thinks most things are superfluous. Kotlin Apprentice raywenderlich.com 5
About the Artist Vicki Wenderlich is the designer and artist of the cover of this book. She is Ray’s wife and business partner. She is a digital artist who creates illustrations, game art and a lot of other art or design work for the tutorials and books on raywenderlich.com. When she’s not making art, she loves hiking, a good glass of wine and attempting to create the perfect cheese plate. Kotlin Apprentice raywenderlich.com 6
Dedications “To my wife Lilia, for all her support, encouragement, and patience. Love you. ! ” —Ellen Shapiro “To my loved ones for their support.” —Irina Galata “To everyone who has come before me to make this book a reality.” —Victoria Gonda Kotlin Apprentice raywenderlich.com 7
Table of Contents: Overview Book License 19................................................................................. Before You Begin 20................................................................ What You Need 21........................................................................................ Book Source Code & Forums 22............................................................. About the Cover 23...................................................................................... Introduction 24.............................................................................................. Section I: Kotlin Basics 25...................................................... Chapter 1: Your Kotlin Development Environment 26..... Chapter 2: Expressions, Variables & Constants 40............. Chapter 3: Types & Operations 64............................................. Chapter 4: Basic Control Flow 82.............................................. Chapter 5: Advanced Control Flow 100.................................. Chapter 6: Functions 114.............................................................. Chapter 7: Nullability 126............................................................. Section II: Collections & Lambdas 137.............................. Chapter 8: Arrays & Lists 138...................................................... Chapter 9: Maps & Sets 157......................................................... Chapter 10: Lambdas 169............................................................. Section III: Building Your Own Types 183........................ Chapter 11: Classes 184................................................................ Chapter 12: Objects 199................................................................ Kotlin Apprentice raywenderlich.com 8
Chapter 13: Properties 211.......................................................... Chapter 14: Methods 228............................................................. Chapter 15: Advanced Classes 240........................................... Chapter 16: Enum & Sealed Classes 261................................. Chapter 17: Interfaces 280........................................................... Chapter 18: Generics 291............................................................. Section IV: Intermediate Topics 328.................................. Chapter 19: Kotlin/Java Interoperability 329....................... Chapter 20: Exceptions 359......................................................... Chapter 21: Functional Programming 372............................. Chapter 22: Conventions & Operator Overloading 398... Chapter 23: Kotlin Coroutines 411........................................... Chapter 24: Scripting with Kotlin 430...................................... Chapter 25: Kotlin/Native 456.................................................... Chapter 26: Kotlin Multiplatform 465..................................... Conclusion 490.............................................................................................. Kotlin Apprentice raywenderlich.com 9
Table of Contents: Extended Book License 19. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Before You Begin 20. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . What You Need 21. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Book Source Code & Forums 22. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . About the Cover 23. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Introduction 24. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Section I: Kotlin Basics 25. . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 1: Your Kotlin Development Environment 26. . . . . . . . . Getting started with IntelliJ IDEA 27. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Book sample projects 35. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Challenges 38. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 39. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 39. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 2: Expressions, Variables & Constants 40. . . . . . . . . . . . . How a computer works 41. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Getting started with Kotlin 47. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Printing out 49. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Arithmetic operations 50. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Math functions 55. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Naming data 56. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Increment and decrement 59. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Challenges 61. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 63. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 63. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 3: Types & Operations 64. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Kotlin Apprentice raywenderlich.com 10
Type conversion 65. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Strings 69. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Strings in Kotlin 71. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Pairs and Triples 74. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Number types 77. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Any, Unit, and Nothing Types 78. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Challenges 79. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 81. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 81. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 4: Basic Control Flow 82. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Comparison operators 83. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The if expression 87. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Loops 92. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Challenges 96. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 98. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 99. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 5: Advanced Control Flow 100. . . . . . . . . . . . . . . . . . . . . . . . Ranges 101. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . For loops 101. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . When expressions 106. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Challenges 111. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 112. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 113. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 6: Functions 114. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Function basics 115. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Functions as variables 120. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Challenges 123. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 125. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 125. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 7: Nullability 126. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Kotlin Apprentice raywenderlich.com 11
Introducing null 127. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Introducing nullable types 128. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Checking for null 130. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Safe calls 133. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Elvis operator 134. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Challenges 135. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 136. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 136. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Section II: Collections & Lambdas 137. . . . . . . . . . . . . . . . Chapter 8: Arrays & Lists 138. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Arrays 139. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Lists 143. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Accessing elements 144. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Modifying lists 149. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Updating elements 151. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Iterating through a list 152. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Nullability and collection types 153. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Challenges 154. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 156. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 156. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 9: Maps & Sets 157. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating maps 158. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Accessing values 159. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Modifying mutable maps 160. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Iterating through maps 162. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Running time for map operations 162. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Sets 164. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating sets 164. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Accessing elements 165. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Adding and removing elements 165. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Kotlin Apprentice raywenderlich.com 12
Running time for set operations 166. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Challenges 166. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 168. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 168. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 10: Lambdas 169. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Lambda basics 170. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Custom sorting with lambdas 175. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Iterating over collections with lambdas 176. . . . . . . . . . . . . . . . . . . . . . . . . . . . . Challenges 180. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 181. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 182. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Section III: Building Your Own Types 183. . . . . . . . . . . . . Chapter 11: Classes 184. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating classes 185. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Reference types 186. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Understanding state and side effects 192. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Data classes 193. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Challenges 197. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 198. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 198. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 12: Objects 199. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Singletons 200. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Getting started 201. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using static members 204. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using anonymous objects 208. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Challenges 209. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 210. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 210. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 13: Properties 211. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Kotlin Apprentice raywenderlich.com 13
Constructor properties 212. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Property initializers 214. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Custom accessors 215. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Companion object properties 218. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Delegated properties 220. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . lateinit 224. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Extension properties 225. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Challenges 226. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 227. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 227. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 14: Methods 228. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Method refresher 229. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Introducing this 232. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Object methods 233. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Extension methods 235. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Challenges 237. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 239. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 239. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 15: Advanced Classes 240. . . . . . . . . . . . . . . . . . . . . . . . . . . . . Introducing inheritance 241. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Sealed classes 251. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Secondary constructors 252. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Nested and inner classes 253. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Visibility modifiers 254. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . When and why to subclass 256. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Challenges 259. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 260. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 260. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 16: Enum & Sealed Classes 261. . . . . . . . . . . . . . . . . . . . . . . . Creating your first enum class 262. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Kotlin Apprentice raywenderlich.com 14
Enum class properties and functions 266. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using when with enum classes 270. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Sealed classes vs. enum classes 272. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Enumeration as state machine 275. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Nullables and enums 277. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Challenges 278. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 279. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 279. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 17: Interfaces 280. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Introducing interfaces 281. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Interface inheritance 286. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Implementing multiple interfaces 287. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Interfaces in the standard library 287. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Challenges 289. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 290. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 290. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 18: Generics 291. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Anatomy of standard library generic types 292. . . . . . . . . . . . . . . . . . . . . . . . . Extension functions on types with generic constraints 298. . . . . . . . . . . . . Creating your own generic constraints 300. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Generic interfaces 309. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Type erasure 312. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Generic type variance (a.k.a., in and out declarations) 320. . . . . . . . . . . . . . Challenges 325. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 327. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 327. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Section IV: Intermediate Topics 328. . . . . . . . . . . . . . . . . . . Chapter 19: Kotlin/Java Interoperability 329. . . . . . . . . . . . . . . . . . . Mixing Java and Kotlin code 330. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Java nullability annotations 341. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Kotlin Apprentice raywenderlich.com 15
Making your Kotlin Code Java-friendly 345. . . . . . . . . . . . . . . . . . . . . . . . . . . . . Accessing nested Kotlin objects 350. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . “Static” values and functions from Kotlin 353. . . . . . . . . . . . . . . . . . . . . . . . . . . Challenge 357. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 358. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 358. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 20: Exceptions 359. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . What is an exception? 360. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Throwing exceptions 363. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Handling exceptions 365. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating custom exceptions 366. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Difference between Java and Kotlin exceptions 369. . . . . . . . . . . . . . . . . . . . Challenges 370. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 371. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 371. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 21: Functional Programming 372. . . . . . . . . . . . . . . . . . . . . . What is functional programming? 373. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Getting started 373. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . First-class and higher-order functions 377. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Lambdas 380. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Extension functions 383. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Lambdas with receivers 385. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Anonymous functions 386. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Returning from lambdas 387. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Inline functions 388. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Tail recursive functions 391. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Collections standard library 392. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Infix notation 393. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Sequences 394. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Challenges 396. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 396. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Kotlin Apprentice raywenderlich.com 16
Where to go from here? 397. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 22: Conventions & Operator Overloading 398. . . . . . . . What is operator overloading? 399. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Getting started 399. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using conventions 400. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Handling collections 404. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Operator overloading and Java 408. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Delegated properties as conventions 409. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Challenges 410. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 410. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 410. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 23: Kotlin Coroutines 411. . . . . . . . . . . . . . . . . . . . . . . . . . . . . Asynchronous programming 412. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Coroutines 414. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Getting started 414. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Configuring coroutines 416. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . CoroutineScope 417. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Coroutines builders 418. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Example: A high-rise building 420. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Error handling 426. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Understanding coroutines 427. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Challenges 428. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 428. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 429. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 24: Scripting with Kotlin 430. . . . . . . . . . . . . . . . . . . . . . . . . . What is scripting? 431. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Installing Kotlin for scripting 432. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Using the REPL 434. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Creating script files 438. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Handling arguments 444. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Kotlin Apprentice raywenderlich.com 17
Getting information from the system 447. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Challenges 454. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 455. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 455. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 25: Kotlin/Native 456. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Konan and LLVM 457. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Installation 457. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Hello, Kotlin/Native 460. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Kotlin Standard Library 462. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Challenges 463. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 464. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 464. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Chapter 26: Kotlin Multiplatform 465. . . . . . . . . . . . . . . . . . . . . . . . . . The KMP Approach 466. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . HelloKMP 469. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Shared project 471. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . expect and actual 475. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Building your projects 477. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Shared code from Android 479. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . The iOS app 480. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Shared code from iOS 487. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Challenge 488. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Key points 489. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Where to go from here? 489. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Conclusion 490. . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . . Kotlin Apprentice raywenderlich.com 18
LBook License By purchasing Kotlin Apprentice, you have the following license: • You are allowed to use and/or modify the source code in Kotlin Apprentice in as many apps as you want, with no attribution required. • You are allowed to use and/or modify all art, images and designs that are included in Kotlin Apprentice in as many apps as you want, but must include this attribution line somewhere inside your app: “Artwork/images/designs: from Kotlin Apprentice, available at www.raywenderlich.com”. • The source code included in Kotlin Apprentice is for your personal use only. You are NOT allowed to distribute or sell the source code in Kotlin Apprentice without prior authorization. • This book is for your personal use only. You are NOT allowed to sell this book without prior authorization, or distribute it to friends, coworkers or students; they would need to purchase their own copies. All materials provided with this book are provided on an “as is” basis, without warranty of any kind, express or implied, including but not limited to the warranties of merchantability, fitness for a particular purpose and noninfringement. In no event shall the authors or copyright holders be liable for any claim, damages or other liability, whether in an action of contract, tort or otherwise, arising from, out of or in connection with the software or the use or other dealings in the software. All trademarks and registered trademarks appearing in this guide are the properties of their respective owners. raywenderlich.com 19
Before You Begin This section tells you a few things you need to know before you get started, such as what you’ll need for hardware and software, where to find the project files for this book, and more. raywenderlich.com 20
The above is a preview of the first 20 pages. Register to read the complete e-book.