6월, 2026의 게시물 표시

Swift Basics: [Advanced - Functions] Default Parameters & Argument Labels

⚙️ Default Parameter Values A default parameter value allows a parameter to automatically assume a specific value if no argument is passed in when the function is called. To declare a default parameter, simply specify the type after the parameter name and assign the default value to it. Tip: It is highly recommended to place parameters with default values at the very end of your parameter list. Swift // Specify the type after the parameter name and assign the default value func functionName ( param1Name : Param1Type , param2Name : Param2Type = DefaultValue ...) -> ReturnType { // Function implementation return returnValue } Let's look at an example. Below, we've set the default parameter value for me to "Jeamin" . Swift // A default value is assigned after the 'me: String' parameter type func greeting ( friend : String , me : String = "Jeamin" ) { print ( "Hello \(friend) ! I am \(me) ." ) } Now let's call t...

Swift Basics: A Quick Comparison of Class, Struct, and Enum

🏛️ CLASS In Swift, we generally use the term instance instead of object. In other words, an instance of a class type is not strictly referred to as an object. Supports only single inheritance . Has (Instance/Type) Methods and (Instance/Type) Properties (same as Structs). It is a Reference Type . Most of the traditional iOS frameworks (like UIKit) are heavily composed of classes. (Note: In contrast, SwiftUI is predominantly composed of Structs.) 🧱 STRUCT Unlike Classes, inheritance is not possible . Has (Instance/Type) Methods and (Instance/Type) Properties (same as Classes). It is a Value Type . The core backbone of Swift's standard library is mostly made up of Structs. Basic data types like Int , Double , and String are actually Structs under the hood. 💡 When to use Structs: When you want to encapsulate a few related data values into a single compound type. When you want the encapsulated data to be copied rather than referenced when passed to another function or assigned to ...