Swift Programming Language Questions - The Basics
23 May 2015
I’ve been playing around with Swift and going through Apple’s Language Guide for Swift. I created some quiz questions for myself based on the first chapter. These questions do not contain all the topics covered in the chapter. For example, booleans are excluded as I am comfortable using them.
All of these questions can be done using a XCode Playground. My answers are below the list of questions.
Questions
-
Declare a variable named ‘x’ and set it to 3
-
Declare a constant named ‘y’ and set it to 5
-
Declare a variable named ‘name’ of type String
-
Set the ‘name’ variable to Fred Flintstone
-
Print “hello” and your name
-
Represent the number 42 as a decimal, binary, octal and hexadecimal number. See Conversion Table - Decimal, Hexadecimal, Octal, Binary for conversion table.
-
Represent the number 1,500,000 as an exponential number
-
Sum these two variable: var a = 5 and var b = 0.25
-
Create a tuple containing a birth date (year, month and day) and then print those values
-
Using the birth date tuple created above get only the year and month and ignore the day
-
Create an optional variable named userInput of type String and set it to have no value
-
Set the userInput variable above to some value and use an if statement and forced unwrapping to print that value
-
Do the above step again but this time use optional binding instead of forced unwrapping
-
What is an implicitly unwrapped optional? When is it used?
-
Write an assert statement to check that a variable named ‘someVar’ is greater than 0.
Answers
1. We don’t need to declare a type as it is inferred from the value (In this case an Int)
var x = 3
2.
let y = 5
3.
var name : String
4.
name = "Fred Flintstone"
5.
println("hello, \(name)")
6.
let decimalNum = 42
let binaryNum = 0b101010
let octNum = 0o052
let hexNum = 0x2A
7.
let num = 1.5e6
8.
var a = 5
var b = 0.25
let result = Double(a) + b
9.
//Possible solution 1
//In this solution the tuple elements are accessed via index.
let birthday = (2015, "January", 23)
println("\(birthday.1) \(birthday.2), \(birthday.0)")
//Possible solution 2
//In this solution the elements are named and accessed via their name
let birthday = (year: 2015, month: "January", day: 23)
println("\(birthday.month) \(birthday.day), \(birthday.year)")
//Possible solution 3
//In this solution variables are assigned to the elements
let birthday = (2015, "January", 23)
var (birthYear, birthMonth, birthDay) = birthday
println("\(birthMonth) \(birthDay), \(birthYear)")
10.
let birthday = (2015, "January", 23)
var (birthYear, birthMonth, _) = birthday
println("\(birthMonth) \(birthYear)")
11. An optional value says the variable has a value and it is X OR there is not a value.
var userInput : String?
12.
var userInput : String?
userInput = "foo"
if userInput != nil {
println("The user input was \(userInput!)")
}else {
println("The user did not input a value")
}
13.
var userInput : String?
userInput = "foo"
if let userInputHasValue = userInput {
println("The user input was \(userInputHasValue)")
}else {
println("The user did not input a value")
}
14. Implicitly unwrapped optionals are written with an exclamation mark rather than a question mark (Double! vs Double?). They are used when it is clear the optional will always have a value. For example, the optional’s value is set immediately after declaring it. The following is an example.
var requiresExpeditedShipping = true //hardcoded here but we can imagine it calls some function that determines this value
let shippingAmount : Double! //the amount is set immediately below so we can use an "implicitly unwrapped optional"
if requiresExpeditedShipping {
shippingAmount = 5.0
}else {
shippingAmount = 2.5
}
println("Value is \(shippingAmount)")
15.
assert(someVar > 0, "someVar not greater than 0")