Let's understand the Optional (Wrapped) type!

Now that we've learned about the Optional type, we'll output it.

What is the Optional (Wrapped) type?

*** Optional (Wrapped) In a nutshell, a type that represents either a value or an empty type. *** *** Basically, swift variables and constants basically do not allow nil, but when using that nil *** Use Optional (Wrapped) type! ***

For example

qiita.rbvar


1.var n: Int
2.
3.print(n)//error

As mentioned above, an error will occur if you try to output a value that does not exist. That's where the Optional (Wrapped) type comes in.

qiita.rbvar


1.var n: Optional<Int>
2.
3.print(n)//nil

As mentioned above, if you use the Optional (Wrapped) type, no error will occur and nil will be output.

In this way, an error occurs because the behavior is *** "If you wrap the data with" Optional type "***, " nil " will be returned if the value does not exist." Can be avoided. This is the basic usage of *** "Optional type" ***.

What is Optional (Wrapped) type unwrap?

Optional (Wrapped) types may not have values, so they cannot be treated in the same way as Wrapped type variables and constants. For example, Int? Four arithmetic operations between types will result in an error.

qiita.rbvar


1.let a: Int? = 1
2.let b: Int? = 1
3.a+b//error

Unwrap to avoid this error. There are three ways to unwrap.

○ *** Optional binding *** ○ ? ?? operator ○ *** Forced unwrap ***

Let's dig deep one by one!

What is an optional binding?

qiita.rbvar


if let constant name= Optional(Wrapped)Type{

//Statement executed if the value exists

}

As in the above statement, if you use an if-let statement and have a Wrapped type value, the {} statement is executed. In the following example, since the constant A has a value, the value is assigned to the constant a of type String and the executable statement is executed!

qiita.rbvar


1.let A: Optional("1") //Int type
2.if let A {
print(type(of:1))

//Execution result: Int

}

What is the ?? operator?

In the following example ,? ?? String type with the String type value "a" on the left side of the operator? The constant optionalString of is specified as the String type value "b" on the right side, and as a result, the value "a" on the left side is acquired.

qiita.rbvar


1.let optionalString:String? = "a"
2.if String = optionalString ?? "b"

//Execution result a

What is forced unwrap?

Forced unwrap is a method of forcibly extracting the value of Wrapped type from Optional (Wrapped) type. To do a forced unwrap! Use the operator. I mentioned above that you can't treat it like a Wrapped variable or constant, but you can retrieve it by using forced unwrap.

qiita.rbvar


let a : Int? = 1
let b : Int? = 1
a!+b! = //2

Recommended Posts

Let's understand the Optional (Wrapped) type!
Let's understand the function!
Let's understand the if statement!
Let's understand the for-in statement!
Declare the model as an optional type
Java Optional type
Let's understand closures!
[Swift] Perform error handling with Optional <Wrapped> type
Let's attack the vulnerability (1) OS command injection (OGNL type injection)
null specifies the type
Let's make the app better
Let's try the S2Struts tutorial (# 3_180425)
Let's solve the roman numerals
Understand the helper method form_with
Let's try the S2Struts tutorial (# 5_180526)
Let's try the S2Struts tutorial (# 4_180505)
About the explanation about functional type
Let's solve the FizzBuzz problem!
Let's try the S2Struts tutorial (# 2_180424)
[Android / Java] Understand the description type in listener settings (button installation)