Look at the following two scenarios with same code:
Using IF LET:
public func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?){
if let error = error {
print("error: \(error.localizedDescription)")
return
}
for service in peripheral.services!
{
print("discovered service is ::::",service)
}
}
Using GUARD LET:
public func peripheral(_ peripheral: CBPeripheral, didDiscoverServices error: Error?){
guard let _ = error else{
for service in peripheral.services!
{
print("discovered service is ::::",service)
}
return
}
}
Both of them are using return statement
and both of them serve the same purpose then what are the differences and which one is better?
EDITS:-
Problem is for general statement it's fine to use any of them but when we have error to handle which one is best to use ?