Skip to content

An optionals extension to unwrap a value using try catch.

License

Notifications You must be signed in to change notification settings

GeRryCh/swift-tryOptional

Folders and files

NameName
Last commit message
Last commit date

Latest commit

 

History

3 Commits
 
 
 
 
 
 
 
 
 
 

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

License

Stars

Watchers

Forks

Releases

No releases published

Packages

No packages published

Languages