In: Computer Science
13. (6 point) when a downcast is necessary what would be the problem if the downcast was not done? Use an example in your explanation.
14. (6 point) The following code is from the ColorViews in-class example. what is the purpose of the function ? what the base class for the type the function is found in? How is it called?
override func draw(_ rect: CGRect)
{
// Get the context being draw upon
let context = UIGraphicsGetCurrentContext( )
//Create layer, if necessary
checkDrawLayer (context)
…
SOLUTION:
Downcasting::--
In general words we can define Upcasting is "Converitng different different memories to common memory."
Downcasting is converting common memory to different different memories.
**********************************************************************************************************************************
import UIKit //here i am importiong all the UI components
let myButton =UIButton()
let mySlider=UISlider()
let myTextField=UITextField()
let myDatePicker=UIDatePicker() // these all are 4 types of Objects
inherited from UIContol class
let controls =[myButton,mySlider,myTextField,myDatePicker];
for item in controls{
if item is UIDatePicker{ // WE are checking the type with
"is"
println("we have UIDatePicker")
// downcast with
"as"
// array
controls is having different types of objects
// if you want
to use any of the object in array property you must down cast the
object to "required object"
let picker =
item as UIDatePicker //Downcastiong
picker.datePickerMode=UIDatePickerMode.CountDownTimer
}
}
Answer to 14th question:
in UIKit we have UIView class in this class we have property
.backgroundColor property
if when UIKit displaying the view it checks for the
.backgroundColor
and fills the backgroundColor.
when you set the the property from UIColor.green.set() and rect.fill() it wont change the background color of the view
when you override draw(_rect: CGRect) function our
code ""draws a filled rectangle""
as UIKit already done the preprocessing of .backgroundColor
property.
as you can see the above example the base class is UIView for the draw(_rect: CGRect).