2

I found this answer to generate random bytes in Swift.

Since result can also have another value than errSecSuccess I wonder in what instance this function would ever fail.

I am strongly thinking about force-unwrapping the return type there because it would be tedious to check for empty data each time it's used. What prevents me from doing just that is that I run this function on app launch and of course having the app crashed on first launch is not very favorable.

Is it safe to assume that this function never fails (at least it never did for me) or are there one-in-a-million cases where this can actually happen so safeguarding it with a retry button would be strongly advised?

tw33
  • 31
  • 3
  • 1
    Good question. I don’t know the answer, but according to https://github.com/EntropyString/EntropyString-Swift it can fail “if the system entropy pool lacks sufficient randomness.” – Martin R Jul 11 '21 at 17:15
  • Interesting, they say `arc4random_buf does not fail` so that's probably good enough for a backup. Maybe it's literally just an edge case shortly after setting up the device etc. so the error will be thrown for internal OS stuff at most – tw33 Jul 11 '21 at 17:26

0 Answers0