diff LazyBear/Global functions/UnwrapAnyOptional.swift @ 448:f71761f166f2

Handle when data is empty
author Dennis Concepción Martín <dennisconcepcionmartin@gmail.com>
date Wed, 23 Jun 2021 11:47:14 +0200
parents
children
line wrap: on
line diff
--- /dev/null	Thu Jan 01 00:00:00 1970 +0000
+++ b/LazyBear/Global functions/UnwrapAnyOptional.swift	Wed Jun 23 11:47:14 2021 +0200
@@ -0,0 +1,21 @@
+//
+//  UnwrapAnyOptional.swift
+//  LazyBear
+//
+//  Created by Dennis Concepción Martín on 23/6/21.
+//
+
+import SwiftUI
+
+/*
+ Unwrap optional Int, Double, String into String
+ */
+func unwrapAnyOptional(value: Any) -> String? {
+    if let value = value as? Int {
+        return "\(value)"
+    } else if let value = value as? Double {
+        return String(format: "%.3f", value)
+    } else {
+        return value as? String
+    }
+}