Location via proxy:   [ UP ]  
[Report a bug]   [Manage cookies]                

p05 Exercises

Download as docx, pdf, or txt
Download as docx, pdf, or txt
You are on page 1of 5

C348 iPhone Programming P05 Exercises

Diploma in Mobile Software Development (School of Infocomm) Page 1

Question
The partial screen capture shows a view controller which has a UILabel and a UIStepper. The code for the view
controller is given below.
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var labelForStepper: UILabel!
@IBOutlet weak var stepper: UIStepper!

override func viewDidLoad() {


super.viewDidLoad()
}

@IBAction func stepperAction(_ sender: UIStepper) {


}
}

1. Explain two ways to set the maximum value of the UIStepper object.
One way is by using the attribute inspector and changing the value directly from there

Another way is by adding stepperAction.maximumValue = desired value

2. When the stepper buttons are pressed, the label is updated with the value of the stepper as shown in the
screenshot. Complete the implementation of the IBAction method stepperAction.
@IBAction func stepperAction(_ sender: UIStepper) {
let value = sender.value
labelForStepper.text = "Stepper value is \(value)"

3. Explain what happens when the maximum value of the stepper is reached.
You can no longer press the + button on the stepper
Question
The partial screen capture shows a view controller which has a UITextField, a UILabel and a UIButton. The code for
the view controller is given below. For each question in this section, provide an answer in the space provided.
import UIKit
class ViewController: UIViewController {
@IBOutlet weak var textfield: UITextField!
@IBOutlet weak var label: UILabel!
@IBOutlet weak var button: UIButton!

override func viewDidLoad() {


super.viewDidLoad()
}

@IBAction func buttonAction(_ sender: UIButton) {


}
}

4. Which inspector would you use in Xcode to check if the UITextField object has an IBOutlet?
Connection inspector

5. When the button is pressed, the label is updated with the value of the text field as shown in the screenshot. In
addition, any keyboard which is currently shown is dismissed. Complete the implementation of the IBAction
method buttonAction.
@IBAction func buttonAction(_ sender: UIButton) {
let textValue = textfield.text
label.text = textValue

6. Explain if it is necessary to have an IBOutlet property for the UIButton object in this scenario.
No need, it is because the button have declared a IBAction property
C348 iPhone Programming P05 Exercises
Diploma in Mobile Software Development (School of Infocomm) Page 2
C348 iPhone Programming P05 Exercises
Diploma in Mobile Software Development (School of Infocomm) Page 3

Question
For each question in this section, provide an answer in the space provided.
func methodA() -> String {
return "Alex"
}

func methodB() -> String? {


return "Alex"
}

func methodC() -> String! {


return "Alex"
}

7. Write the code to call methodA and print the returned value.

var x : String = methodA()

8. Write the code to call methodB and print the returned value. Your code should not crash if the returned value is
nil. Your code should print a String value.
Using optional binding
if let methodB = methodB(){
print (methodB)
}

Not using optional binding,


check for not nil value if (methodB() != nil){
print (3ethod()!)
}

9. Write the code to call methodC and print the returned value. Your code may assume that methodC will never
return a nil value, and thus should not use optional binding or check for not nil.

print(methodC()!)

Question
10. Write the code to print the output shown below to the debug area using a for loop:
1 Hello 2
2 Hello 3
3 Hello 4

for i in 1...3{
print( "\(i) Hello \(i+1)")
}

11. Write the code to print the output shown below to the debug area using a for loop:
2*2=4
3*3=9
4 * 4 = 16

for i in 2...4{
print( "\(i) * \(i) = \(i*i) ")
}
C348 iPhone Programming P05 Exercises
Diploma in Mobile Software Development (School of Infocomm) Page 4
C348 iPhone Programming P05 Exercises
Diploma in Mobile Software Development (School of Infocomm) Page 5

Question
12. Write a function with the following requirements:
 the function has two parameters
 the first parameter is an optional String and is the name of the person
 the second parameter is an optional Int and is the age of the person
 the function returns an optional String
 the returned String is of the format: <name> is <age> years old. (e.g. John Tan is 12 years old.)
 the returned value is nil if either of the two parameters are nil

13. Call the function above and print the result so that the output appears as shown below.
John is 12 years old.

var name : String? = nil


var age : Int? = nil

name = "John"
age = 12
if let name = name, let age = age{
print("\(name) is \(age) years old.")
}

End of Exercises

You might also like