Swift language:
Explain the concept of optional-chaining to print "megaPixel" value of camera by using the following example code only
struct Person {
var name: String
var phone: Phone?
}
struct Phone {
var camera: Camera?
}
struct Camera {
var megaPixel: Int
}
Answers
Answered by
0
sorry too big
omg oh my god
Answered by
1
Answer:
struct Person {
var phone: Phone?
}
struct Phone {
var camera: Camera?
}
struct Camera {
var megaPixel: Int = 40
}
var cand = Person()
cand.phone = Phone()
if let megapixel = cand.phone?.camera?.megaPixel {
print("MexaPixel value is \(megapixel)")
}
else {
print("MegaPixel value of Camerae cannot be retreived")
}
Explanation:
Similar questions
Computer Science,
3 months ago
Science,
3 months ago
Computer Science,
3 months ago
Science,
7 months ago
Math,
7 months ago
Political Science,
11 months ago
Political Science,
11 months ago