I got this
#if os(iOS)
import UIKit
import Foundation
#elseif os(OSX)
import Foundation
import cocoa
#else
// Future concerns
#endif
enum Image: String {
case Preferences
....
case App
case Stop
....
case Default
#if os(iOS)
func image(selected: Bool = false) -> UIImage? {
let imgName : String = selected ? self.rawValue + "-selected" : self.rawValue
if let img = UIImage(named:imgName) {
return img
} else {
print("iOS")
print("Please add the \(imgName) icon to the app assets!!")
//Creating the alert
let alertController = UIAlertController(title: "Icon Missing", message: "Please add the \(imgName) image to the app assets!!", preferredStyle: .alert)
let action = UIAlertAction(title: "I Promiss I will", style: .default, handler: nil)
alertController.addAction(action)
// How to get the alert working
self.window?.rootViewController?.presentViewController(alertController, animated: true, completion: nil)
//
return nil
}
}
#elseif os(OSX)
func image(selected: Bool = false) -> NSImage? {
....
}
#else
//
#endif
}
I know enum doesn't have a self.window, I just type this line (self.window?.rootViewController?...) to show the issue. Is there away to get the alert working? Of course I don't want to submit a view object to the Image.Question.image(). Also if it will be used in a later phase in the development will be debatable, but still want to know if there is a way.
Thank you in advance.
======== Solved =========
With the info and comment from k.zoli, my main issue did get solved.
resulting code
func image(selected: Bool = false) -> UIImage? {
let imgName : String = selected ? self.rawValue + "-selected" : self.rawValue
guard let img = UIImage(named: imgName) else {
print("iOS")
print("Please add the \(imgName) icon to the app assets!!")
//Creating the alert
let alertController = UIAlertController(title: "Icon Missing", message: "Please add the \(imgName) icon to the app assets!!", preferredStyle: .alert)
let action = UIAlertAction(title: "I Promiss I will", style: .default, handler: nil)
alertController.addAction(action)
if let window = UIApplication.shared.delegate?.window { DispatchQueue.main.async { window?.rootViewController?.present(alertController, animated: true, completion: nil) } }
return nil
}
return img
}