![]() ![]() ![]() ![]() Output : Nil Unwrap an optional type with forced unwrappingįorced unwrapping is quite contradictory because you're accessing the optional value regardless of its value (nil or not nil). You can do this by using a simple if-else block like this: var variable:String? //evaluates to nil Unwrapping means to make sure that the Optional value is not nil. Unwrap an optional type with an if-else block You can perform unwrapping in the following ways: Unwrapping in Swift is essentially verifying if the Optional value is nil or not, and then it performs a task only if it's not nil. Only when you open the box (unwrapping) can you learn if the cat is alive or not (nil or not). This means that it is both dead and alive at the same time (nil and non-nil) according to your perception. Unless you open the box, you can't assure whether the cat is dead or alive ( nil or non-nil). In this thought experiment, a cat is placed in a closed box along with a vial of poison. You can better understand this concept by thinking about Schrödinger's cat. This casting is what's known as unwrapping. You won't use optional types upfront for any operations or tasks, as they should be cast in their primitives or user-defined instances before using it elsewhere (an optional string should be cast to a string, an optional integer should be cast to an integer, and so on). You can see that the outputs are not regular values. The declaration is similar to declaring regular variables, except that you add a question mark (?) beside the data type which makes it an Optional type.įire up your XCode playground and try running these snippets: let someVal:Double? This is because primitive data types cannot handle nil values, but Optional types can. For instance, an Optional String is not the same as a string, an Optional integer is not the same as an integer, and so forth. So optionals are basically used to handle null values at compile-time to ensure that no crashes occur at runtime.Īny operations on the optional variable are performed only if it contains non-null values.Īn optional type can be of any data type, like a String, Integer, Double, Float, or any user defined non-primitive data type (object).īut, it is important to note that the Optional data type is not equivalent to its base data type. ![]() That's a pretty straightforward definition. An optional represents two possibilities: Either there is a value, and you can unwrap the optional to access that value, or there isn’t a value at all. You use optionals in situations where a value may be absent. Let's see what the docs have to say about it: Optional types or Optionals in SwiftĬoming to the main point, what are optional types in Swift? You might be wondering, if you can omit the data type then what's the need to specify it anyway? Well, you're right to wonder that – but you need to specify the data type once you're working with optional types, which are different from conventional data types. It can make appropriate assumptions based on the assigned value, for example: Swift is a type-safety language, which means that you can assign variables and constants values without specifying the data type. The keyword is then followed by the constant/variable name, a colon and its data type, and then the assigning value. Notice that of the keywords we've used here for declaring constants and variables, you use let to declare a constant, and var to declare a variable. Message = "Coding in swift is awesome" //variable value changed. Let's look at that syntax:Īnd here's an example of a variable: var message:String = "Hello, this is prajwal" There are some nuances in how declare constants and variables in Swift. On the other hand, a variable is a data item whose value can be changed limitlessly. Constants and Variables in SwiftĪ constant is a data item whose value, once assigned, cannot be mutated (modified or changed) throughout the scope of the program. To understand what optional types are, let's quickly brush up on constants and variables. If you're just getting started with Swift and you're learning about optional types for the first time, make sure to read this article until the end. Optionals are a fundamental topic that you need to thoroughly understand to code in Swift. And you might be quite surprised to know that such a concept exists in Swift. If you're coming from Java, C++, or other object-oriented languages, chances are that you've never come across optional types or "optionals". ![]()
0 Comments
Leave a Reply. |