Question

iOS App Development 10. (8 point) What are optionals? Why are optionals needed? Why must unwrapping...

iOS App Development

10. (8 point) What are optionals? Why are optionals needed? Why must unwrapping be done? What can go wrong with optional unwrapping?

12. 6 PointName a swift / iOS type is a value type. Name a Swift/iOS type that is a reference type. Explain what happens when a value type variable is assigned a new value. Do not mention parameter passing in your answer.

0 0
Add a comment Improve this question Transcribed image text
Answer #1

10.

Optionals in Swift are can be having data or no-data

//for the user the input can be optional (he might give data or he can leave it blank)

if we dont use optional then every data input is mandatory

Syntax:

var name: String? or var name : Optional<String>

//above String? , variable name can be of type String or nothing

Unwrapping

Basically feel optionals as a container so until we open the container we dont know what is the data inside the container

to unwrap we use ! at the end of var name

eg : print("My name is \(name!)")

problem with optional is when we assign name=nil

it raises the runtime crash stating nil is unexpected

so,we have to be clear that the optional data is having a valid input

we can use if statements to handle this

eg : if (name != nil) {

print("My name is \(name!)")

}

only when there is data in var name print following statement

we can also use if let statement provided by swift

eg:

if let name: String = name {

   print("My name is \(name!)")

}

Add a comment
Know the answer?
Add Answer to:
iOS App Development 10. (8 point) What are optionals? Why are optionals needed? Why must unwrapping...
Your Answer:

Post as a guest

Your Name:

What's your source?

Earn Coins

Coins can be redeemed for fabulous gifts.

Not the answer you're looking for? Ask your own homework help question. Our experts will answer your question WITHIN MINUTES for Free.
Similar Homework Help Questions
  • Using Swift playground and / or the command line for macOS (open Xcode, create a new...

    Using Swift playground and / or the command line for macOS (open Xcode, create a new Xcode project, macOS, command line tool), practice the following exercises: Exercise: Swift Variables Declare 2 variables/constants with some random values and any name of your choice Considering these 2 variables, one as a value of PI and other as a radius of circle, find the area of circle Print the area of circle Declare a string variable explicitly with value “Northeastern”. Declare a string...

  • Program Overview This brief exercise is designed for you to consider how reference variables behave when...

    Program Overview This brief exercise is designed for you to consider how reference variables behave when you are passing them as arguments by-value. Instructions Name your class References.java. 1. Implement the following methods. 1.1 Method name: readStudentNames Purpose: This method accepts an array of strings as a parameter. It loops through the array and asks the user to input names. It populates the array of strings with the names. Parameters: an array of Strings, stringArray Return type: void In the...

  • use intellij idea main java wp the professor. Please make sure to only implement what is...

    use intellij idea main java wp the professor. Please make sure to only implement what is asked for. You may not add any instance variables to any class, though you may create local variables inside of a method to accomplish its task. No other methods should be created other than the ones listed here. Step 1 Develop the following interface: Interface Name: Queue Interface<T> Access Modifier: public Methods Name: isEmpty Access modifier: public Parameters: none Return type: boolean Name: dequeue...

  • 1. What is an abstract method and why are they useful? Illustrate your answer using an...

    1. What is an abstract method and why are they useful? Illustrate your answer using an example of where you might use an abstract method. 2. What is the difference between a static variable and a non-static variable? Given the example of a class representing Dogs, give an example of a variable that may be static and another that may non-static. 3. In Java, when you modify a String as shown in the code below, Java makes a new String...

  • Mobile Application Development questions Match the component type to the example of that component type.   ...

    Mobile Application Development questions Match the component type to the example of that component type.       -       A.       B.       C.       D.       E.       F.       G.    A Tip Calculator       -       A.       B.       C.       D.       E.       F.       G.    Where’s My App, which waits for a text message to be received...

  • Java Project Requirements: Account class Superclass Instance variables clearPassword String Must be at least 8 characters...

    Java Project Requirements: Account class Superclass Instance variables clearPassword String Must be at least 8 characters long encryptedPassword : String key int Must be between 1 and 10(inclusive) accountId - A unique integer that identifies each account nextIDNum – a static int that starts at 1000 and is used to generate the accountID no other instance variables needed. Default constructor – set all instance variables to a default value. Parameterized constructor Takes in clearPassword, key. Calls encrypt method to create...

  • Question 1 What is the value of x after the following int x = 5; x++;...

    Question 1 What is the value of x after the following int x = 5; x++; x++; x+=x++; A)14 B)10 C)13 D)15 Question 2 The last line in value returning function (before the }) should contain the word return. True False Question 3 This contains three parts: Void or Data Type the name optional parameter list A)Menu System B)Function Header C)Switch Question 4 What is a variable? A)a pointer B)a place in memory to hold data C)int D)a computer programming...

  • Need help problem 9-13 C++ Homework please help WRITE FUNCTION PROTOTYPES for the following functions. The...

    Need help problem 9-13 C++ Homework please help WRITE FUNCTION PROTOTYPES for the following functions. The functions are described below on page 2. (Just write the prototypes) When necessary, use the variables declared below in maino. mm 1.) showMenu m2.) getChoice 3.) calcResult m.) showResult 5.) getInfo mm.) showName 7.) calcSquare 8.) ispositive int main { USE THESE VARIABLES, when needed, to write function prototypes (#1 - #8) double num1 = 1.5; double num2 = 2.5; char choice; double result;...

  • I need help with this assignment. It's due Tuesday at 8 AM. Please follow the instructions thorou...

    I need help with this assignment. It's due Tuesday at 8 AM. Please follow the instructions thoroughly as to not use any advanced material we may not have gone over in class. The instructions let you know what all should be used. Thanks! Use only what you have learned in Chapters 1 - 7. Use of "advanced" material, material not in the text, or project does not follow the instructions, will result in a GRADE OF 0% for the project....

  • To insure that file output is written to the disk you need to execute what method?...

    To insure that file output is written to the disk you need to execute what method? a. commit() b. write() c. close() d. complete() The following is called the ________ of a function. def myFunction(x,y) : a. header b. footer c. sentinel d. index True or False: A commonly used program that uses regular expressions is grep. True or False: In Python exception handling provides a mechanism for passing control from the point of the error detection to a handler...

ADVERTISEMENT
Free Homework Help App
Download From Google Play
Scan Your Homework
to Get Instant Free Answers
Need Online Homework Help?
Ask a Question
Get Answers For Free
Most questions answered within 3 hours.
ADVERTISEMENT
ADVERTISEMENT
ADVERTISEMENT