"swift 5.0: 'withunsafebytes' is deprecated: use `withunsafebytes<r>(…)" Code Answer


in swift 5 the withunsafebytes() method of data calls the closure with an (untyped) unsaferawbufferpointer, and you can load() the value from the raw memory:

let value = data.withunsafebytes { $0.load(as: uint32.self) }

(compare how to use data.withunsafebytes in a well-defined manner? in the swift forum). note that this requires that the memory is aligned on a 4-byte boundary. for alternatives see round trip swift number types to/from data.

note also that as of swift 4.2 you can create a random 32-bit integer simply using the new random api:

let randomid = uint32.random(in: .min ... .max)
By Kasia Kulma on April 5 2022

Answers related to “swift 5.0: 'withunsafebytes' is deprecated: use `withunsafebytes<r>(…)”

Only authorized users can answer the Search term. Please sign in first, or register a free account.