var a : Int // create a swift variable of type int
var b = a + 10 //compiler will thrown an error because a is not initialized
Alternative to above
When your using ‘?”/optional , that means your explicitly telling the compiler that a may or may not have the value.
var a : Int?
var b = a! + 5 // the compiler suggest to unwrap the optional, this is called force unwrapping and program will crash if in case a does not have value so force unwrapping of optionals is always dangerous and should not do it unless your sure about var holding a value.
then what is the safe way to deal with optionals? answer is optional binding
if let nonOptionalValue = a {
var b = nonOptionalValue + 5 // if a is nil, this statement is not executed and force unwrapping is not required in this case because we knew that a is not nil and has a value for sure.
}
Awesome article I really impress it’s very informative and useful.Thanks
ReplyDeleteCustom Software Development Australia
I enjoy reading through your article post, I like this post and it is informative article for all of us. Thanks for sharing your view. - dentist in windsor mill
ReplyDeleteHealthy Dental
https://healthydental.com/
Thanks for Sharing!!!
ReplyDeleteCotton Sarees in Surat
Great post with unique information.This blog will really helpful for me to develop my skills in a right way.Thanks for sharing,keep update with your blogs.
ReplyDeleteWebsite Design Company in Bangalore | Mobile App Development Companies in
Bangalore | Website Development Company in Bangalore