little confuse about optionals in swift

84 Views Asked by At

Maybe I'm very confused idk yet. I thought i understood the basic concept but when playing around in xcode I'm not getting what I thought I should be Take the following code

var title: String? = "hello world"
println("\(title)")

I thought that this would give me an error because I thought that title should have to be unwrapped since I;m accessing it, but it seems to compile just fine. Can someone shed some light on this.

5

There are 5 best solutions below

0
nick9999 On

All an optional is is that if it is nil, the app won't crash. If you put the elimination mark, and did not assign a value, it would crash.

In an if statement, you can check if the variable is nil:

if (title == nil) {

//this works

}
0
Wraithseeker On
var title: String? = "hello world"
println("\(title)") // This would print out Optional(Title)
var title2: String! = "hello world"
println("\(title2)") // This would print in Title

Optional do not cause your program to crash when they are executed without a value but !(variable) will cause your code to crash if they have no value.

Swift documentation:

As described above, optionals indicate that a constant or variable is allowed to have “no value”. Optionals can be checked with an if statement to see if a value exists, and can be conditionally unwrapped with optional binding to access the optional’s value if it does exist.

5
Yaman On

Optionals are Printables, meaning you can print them to an output stream (here the console). You should see something like Optional("hello world") in your console and not hello world

0
Amit89 On

As per documentation, your statement will be converted to

var title: Optional<String> = Some("hello world").

From Apple documentation, Optional is

enum Optional<A> {
case Nil
case Some(A)
}

So when you are passing a value to an optional, at the time of declaration, compiler understands, it has got a value and that's why it is not showing error.

But if you do the below code you may get different result.

var title: String? = "hello world"
title = nil
println("\(title)")

It will print "nil" in playground.

0
Nurdin On

Based on apple doc

The concept of optionals doesn’t exist in C or Objective-C. The nearest thing in Objective-C is the ability to return nil from a method that would otherwise return an object, with nil meaning “the absence of a valid object.” However, this only works for objects—it doesn’t work for structures, basic C types, or enumeration values. For these types, Objective-C methods typically return a special value (such as NSNotFound) to indicate the absence of a value. This approach assumes that the method’s caller knows there is a special value to test against and remembers to check for it. Swift’s optionals let you indicate the absence of a value for any type at all, without the need for special constants.

Example

let possibleString: String? = "An optional string."
let forcedString: String = possibleString! // requires an exclamation mark

let assumedString: String! = "An implicitly unwrapped optional string."
let implicitString: String = assumedString // no need for an exclamation mark

Reference: https://developer.apple.com/library/ios/documentation/Swift/Conceptual/Swift_Programming_Language/TheBasics.html