0

I have the following extension:

extension UserDefaults {
    func color(forKey defaultName: String) -> UIColor? {
        guard let data = data(forKey: defaultName) else {
           return nil
        }
        let size = MemoryLayout<CGFloat>.size
        return UIColor(red:   data[size*0..<size*1].withUnsafeBytes{ $0.pointee },
                   green: data[size*1..<size*2].withUnsafeBytes{ $0.pointee },
                   blue:  data[size*2..<size*3].withUnsafeBytes{ $0.pointee },
                   alpha: data[size*3..<size*4].withUnsafeBytes{ $0.pointee })
    }
}

And Xcode is prompting me with the following warning:

withUnsafeBytes is deprecated: use withUnsafeBytes<R>(_: (UnsafeRawBufferPointer) throws -> R) rethrows -> R instead

for the red, Green and blue parameters.

I have tried to implement the suggested change but Xcode, no matter what, keeps showing me the warning; I might be doing something wrong that I cannot figure out.

I have tried other solutions, as described in other posts here, but it keeps warning me with that message.

I have read a Swift forum thread regarding this and it seems it could be a bug within swiftlang itself.

Does anyone knows a workaround?

Ivan Cantarino
  • 3,058
  • 4
  • 34
  • 73

0 Answers0