Created
January 24, 2020 14:36
-
-
Save alokc83/c45ca9fb0b762883f49c0d5e61e2cc43 to your computer and use it in GitHub Desktop.
use of nil coalescing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import Foundation | |
import UIKit | |
func unwrappingWithOprator(input: String? = nil) { | |
print("\n-----------Result for function \(#function) -----------") | |
let name: String? = input | |
let unwrappedName = name ?? "Unknown Entity" | |
print(unwrappedName) | |
// or you can | |
print(name ?? "Unknown Entity") | |
} | |
unwrappingWithOprator(input: "Hulk") | |
unwrappingWithOprator() | |
func usingNilCoalescingOpratorWithTry(input urlString: String? = nil) { | |
print("\n-----------Result for function \(#function) -----------") | |
let titleText = (try? String(contentsOfFile: "hunlk.info")) ?? "Avengers assemble." | |
print(titleText) | |
} | |
usingNilCoalescingOpratorWithTry() |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment