Skip to content

Instantly share code, notes, and snippets.

@alokc83
Created January 24, 2020 14:36
Show Gist options
  • Save alokc83/c45ca9fb0b762883f49c0d5e61e2cc43 to your computer and use it in GitHub Desktop.
Save alokc83/c45ca9fb0b762883f49c0d5e61e2cc43 to your computer and use it in GitHub Desktop.
use of nil coalescing
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