পৃষ্ঠাসমূহ

Search Your Article

CS

 

Welcome to GoogleDG – your one-stop destination for free learning resources, guides, and digital tools.

At GoogleDG, we believe that knowledge should be accessible to everyone. Our mission is to provide readers with valuable ebooks, tutorials, and tech-related content that makes learning easier, faster, and more enjoyable.

What We Offer:

  • 📘 Free & Helpful Ebooks – covering education, technology, self-development, and more.

  • 💻 Step-by-Step Tutorials – practical guides on digital tools, apps, and software.

  • 🌐 Tech Updates & Tips – simplified information to keep you informed in the fast-changing digital world.

  • 🎯 Learning Support – resources designed to support students, professionals, and lifelong learners.

    Latest world News 

     

Our Vision

To create a digital knowledge hub where anyone, from beginners to advanced learners, can find trustworthy resources and grow their skills.

Why Choose Us?

✔ Simple explanations of complex topics
✔ 100% free access to resources
✔ Regularly updated content
✔ A community that values knowledge sharing

We are continuously working to expand our content library and provide readers with the most useful and relevant digital learning materials.

📩 If you’d like to connect, share feedback, or suggest topics, feel free to reach us through the Contact page.

Pageviews

Wednesday, February 8, 2017

Swift - Optionals

Swift also introduces Optionals type, which handles the absence of a value. Optionals say either "there is a value, and it equals x" or "there isn't a value at all".
An Optional is a type on its own, actually one of Swift’s new super-powered enums. It has two possible values, None and Some(T), where T is an associated value of the correct data type available in Swift.

Here’s an optional Integer declaration −
var perhapsInt: Int?
Here’s an optional String declaration −
var perhapsStr: String?
The above declaration is equivalent to explicitly initializing it to nil which means no value −
var perhapsStr: String?  = nil
Let's take the following example to understand how optionals work in Swift −
import Cocoa

var myString:String? = nil

if myString != nil {
   println(myString)
}else {
   println("myString has nil value")
}
When we run the above program using playground, we get the following result −
myString has nil value
Optionals are similar to using nil with pointers in Objective-C, but they work for any type, not just classes.

Forced Unwrapping

If you defined a variable as optional, then to get the value from this variable, you will have to unwrap it. This just means putting an exclamation mark at the end of the variable.
Let's take a simple example −
import Cocoa

var myString:String?

myString = "Hello, Swift!"

if myString != nil {
   println(myString)
}else {
   println("myString has nil value")
}
When we run the above program using playground, we get the following result −
Optional("Hello, Swift!")
Now let's apply unwrapping to get the correct value of the variable −
import Cocoa

var myString:String?

myString = "Hello, Swift!"

if myString != nil {
   println( myString! )
}else {
   println("myString has nil value")
}
When we run the above program using playground, we get the following result −
Hello, Swift!

Automatic Unwrapping

You can declare optional variables using exclamation mark instead of a question mark. Such optional variables will unwrap automatically and you do not need to use any further exclamation mark at the end of the variable to get the assigned value. Let's take a simple example −
import Cocoa

var myString:String!

myString = "Hello, Swift!"

if myString != nil {
   println(myString)
}else {
   println("myString has nil value")
}
When we run the above program using playground, we get the following result −
Hello, Swift!

Optional Binding

Use optional binding to find out whether an optional contains a value, and if so, to make that value available as a temporary constant or variable.
An optional binding for the if statement is as follows −
if let constantName = someOptional {
   statements
}
Let's take a simple example to understand the usage of optional binding −
import Cocoa

var myString:String?

myString = "Hello, Swift!"

if let yourString = myString {
   println("Your string has - \(yourString)")
}else {
   println("Your string does not have a value")
}
When we run the above program using playground, we get the following result −
Your string has - Hello, Swift!

No comments:

Post a Comment