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
Social Sciences,
3 months ago
English,
3 months ago
Math,
3 months ago
Science,
6 months ago
Math,
6 months ago
Political Science,
10 months ago
Political Science,
10 months ago