Skip to content

Latest commit

 

History

3 Commits

Folders and files

NameName
Last commit message
Last commit date
 
 
 
 
 
 
 
 
 
 

Repository files navigation

tryOptional

An optionals extension to unwrap a value using try catch.

Using

You can use a custom operator ??

//Optional contains value
var a: Int? = 5

do {
    let b = try ??a
    print(b)
} catch let error {
    print(error)
}

prints: 5

//Optional contains nil: Wrapped type is printed
a = nil

do {
    let b = try ??a
    print(b)
} catch let error {
    print(error)
}

prints: Nil returned for optional of type: Int

or an unwrap method directly, which also prints file and line where the error occurs

//Optional contains nil: file+line printed
do {
    let b = try a.unwrap()
    print(b)
} catch let error {
    print(error)
}

prints: Nil returned for optional of type: Int at tryOptionalPlayground.playground line: 25

About

An optionals extension to unwrap a value using try catch.

Resources

Stars

0 stars

Watchers

1 watching

Forks

Releases

Packages

Contributors

Languages