From 4a0c8ada48f5370a1d8e7ca584c9b34b404cf8cf Mon Sep 17 00:00:00 2001 From: ghostjiang Date: Mon, 14 Jan 2019 17:40:16 +0800 Subject: [PATCH] add guard block to prevent possible crash --- Source/iOS/UIColor+Hue.swift | 11 ++++++++--- 1 file changed, 8 insertions(+), 3 deletions(-) diff --git a/Source/iOS/UIColor+Hue.swift b/Source/iOS/UIColor+Hue.swift index 84cfc00..9371486 100644 --- a/Source/iOS/UIColor+Hue.swift +++ b/Source/iOS/UIColor+Hue.swift @@ -21,10 +21,15 @@ public extension UIColor { } } + guard let intCode = Int(hex, radix: 16) else { + self.init(white: 1.0, alpha: 0.0) + return + } + self.init( - red: CGFloat((Int(hex, radix: 16)! >> 16) & 0xFF) / 255.0, - green: CGFloat((Int(hex, radix: 16)! >> 8) & 0xFF) / 255.0, - blue: CGFloat((Int(hex, radix: 16)!) & 0xFF) / 255.0, alpha: 1.0) + red: CGFloat((intCode >> 16) & 0xFF) / 255.0, + green: CGFloat((intCode >> 8) & 0xFF) / 255.0, + blue: CGFloat((intCode) & 0xFF) / 255.0, alpha: 1.0) } /// Adjust color based on saturation