Call function that throws and attempt to cast the returned value, abort silently if either fails

Nicolas Miari

I'm guarding the call to a method that may throw (a CoreData fetch request), and at the same time I'm casting the returned value to a specific type of array:

guard let results = try? managedContext.executeFetchRequest(fetchRequest) as? [MyManagedObjectClass] else {

    // Bail out if fetch or cast fails:
    return
}

The way I understand it is that, because I'm using guard/try?/else (instead of do/try/catch), the else block (i.e. return statement) above will be executed if either of the following occurs:

  1. The fetch request fails (and throws)
  2. The fetch request succeeds but casting of the returned value to [MyManagedObjectClass] fails.

The code above works, but the after control successfully passes the guard check, the variable results ends up as being of type [MyManagedObjectClass]? ("optional array of MyManagedObjectClass").

So if I want to -say- loop through the array elements I have to first unwrap it:

if let results = results {
    // now, 'results' is of type:
    // "(non-optional) Array of MyManagedObjectClass" 

    for element in results {
        // (process each element...)
    }
}

I could use as! instead of as? when casting after the guard statement; that makes results a non-optional array.

But then, if the fetch succeeds but the cast fails, It will still trigger a runtime error (crash) instead of control flowing to the else block, right?.

Is there a smarter way? (catch both errors, end up with a non-optional array)


Note: I understand that lumping both failure causes (fetch and cast) together into one else block is not the best design, and perhaps I should check for both separately (perhaps I should use the more traditional do/try/catch), but I would like to better grasp the details of this complex construct.

Cristik

You need to wrap the try call into parentheses:

let results = (try? managedContext.executeFetchRequest(fetchRequest)) as? [MyManagedObjectClass]

otherwise try? will apply to the whole expression, generating an optional value that gets assigned to results.

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

From Dev

Attempt to call field 'defaultName' (a string value) error

From Dev

Attempt to use @INC and call function on demand

From Dev

Getting function returned value

From Dev

Pandas.to_datetime function fails silently

From Dev

Don't call function if None Value returned

From Dev

attempt to call method 'translet' (a nil value) in corona

From Dev

Luaj attempt to index ? (a function value)

From Dev

attempt to call method 'removeSelf' (a nil value)

From Dev

lua attempt to call global write (a nil value)

From Dev

Bizzare "attempt to call a table value" in Lua

From Dev

EnumMap with LuaJava (attempt to call a nil value)

From Dev

Function returned without value

From Dev

How to make Morphia set the value of a field as the value returned by a function call?

From Dev

Metatables, attempt to call method 'rename' (a nil value)

From Dev

No returned value when calling a class and function containing API call

From Dev

vba Function Returned value after call

From Dev

Lua script throws error "attempt to call a nil value (field 'deposit')"

From Dev

Attempt to call an undefined function glutInit

From Dev

Return Optional as it is if the returned Optional has value, else call another function

From Dev

Attempt to call field 'defaultName' (a string value) error

From Dev

Attempt to use @INC and call function on demand

From Dev

Abort setup accourding function returned value using InstallShield Spring

From Dev

Pandas.to_datetime function fails silently

From Dev

Function as a returned value, Python

From Dev

How to call a returned value to java function here

From Dev

Function returned without value

From Dev

What is the value returned by this function?

From Dev

'resource' function evaluates either way and fails

From Dev

AWS Lambda Node Function Silently Fails

Related Related

  1. 1

    Attempt to call field 'defaultName' (a string value) error

  2. 2

    Attempt to use @INC and call function on demand

  3. 3

    Getting function returned value

  4. 4

    Pandas.to_datetime function fails silently

  5. 5

    Don't call function if None Value returned

  6. 6

    attempt to call method 'translet' (a nil value) in corona

  7. 7

    Luaj attempt to index ? (a function value)

  8. 8

    attempt to call method 'removeSelf' (a nil value)

  9. 9

    lua attempt to call global write (a nil value)

  10. 10

    Bizzare "attempt to call a table value" in Lua

  11. 11

    EnumMap with LuaJava (attempt to call a nil value)

  12. 12

    Function returned without value

  13. 13

    How to make Morphia set the value of a field as the value returned by a function call?

  14. 14

    Metatables, attempt to call method 'rename' (a nil value)

  15. 15

    No returned value when calling a class and function containing API call

  16. 16

    vba Function Returned value after call

  17. 17

    Lua script throws error "attempt to call a nil value (field 'deposit')"

  18. 18

    Attempt to call an undefined function glutInit

  19. 19

    Return Optional as it is if the returned Optional has value, else call another function

  20. 20

    Attempt to call field 'defaultName' (a string value) error

  21. 21

    Attempt to use @INC and call function on demand

  22. 22

    Abort setup accourding function returned value using InstallShield Spring

  23. 23

    Pandas.to_datetime function fails silently

  24. 24

    Function as a returned value, Python

  25. 25

    How to call a returned value to java function here

  26. 26

    Function returned without value

  27. 27

    What is the value returned by this function?

  28. 28

    'resource' function evaluates either way and fails

  29. 29

    AWS Lambda Node Function Silently Fails

HotTag

Archive