In objC,
NSString *stringValue = @"123s";
NSInteger *intValue = [stringValue integerValue];
NSLog(@"intergerValue %@",intValue);
if(!intValue)
{
NSLog(@"caught null object");
}
else
{
// Do appropriate operation with the not null object
}
prints " interValue (null) " " caught null object "
and the binding is done safely by using !(not) operator inside if condition...
But whereas, in swift the equivalent snippet using optional variable is
var normalValue : String = "123s"
var optionalValue = normalValue.toInt()
println("optionvalue \(optionalValue)")
if optionalValue {
// Do appropriate operation with the not nil value
}
else{
println("caught null object")
}
this "optional binding" is done in objectiveC also, then what is the exact use of having optional variable/constant. And it's also been said that we can avoid returning null object instead we can return nil value. What is the problem when we return a null object, does it cause performance issues? Your valid thoughts....