Reputation: 41
I try to set the theme (include the background and a picture) for my app. I try this code:
import UIKit
enum AppTheme {
case dayMode
case nightMode
func setBackground() -> (UIImage, UIImage) {
switch self {
case .dayMode():
let backgroundImage = #imageLiteral(resourceName: "DayMode")
let sunControllerImage = #imageLiteral(resourceName: "sun")
return (backgroundImage, sunControllerImage)
case .nightMode():
let backgroundImage = #imageLiteral(resourceName: "NightMode")
let sunControllerImage = #imageLiteral(resourceName: "moon")
return (backgroundImage, sunControllerImage)
}
}
}
then in viewDidLoad
, I try:
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
(backgroundImg.image, sunControllerImg.image) = AppTheme.nightMode.setBackground()
}
However, there is an error:
Cannot express tuple conversion
(UIImage, UIImage)
to(UIImage?, UIImage?)
(aka(Optional<UIImage>, Optional<UIImage>)
).
Is there anyway to handle this?
Upvotes: 0
Views: 233
Reputation: 10590
try this way
enum AppTheme {
case dayMode
case nightMode
func setBackground() -> (UIImage?, UIImage?) {
switch self {
case .dayMode():
let backgroundImage = #imageLiteral(resourceName: "DayMode")
let sunControllerImage = #imageLiteral(resourceName: "sun")
return (backgroundImage, sunControllerImage)
case .nightMode():
let backgroundImage = #imageLiteral(resourceName: "NightMode")
let sunControllerImage = #imageLiteral(resourceName: "moon")
return (backgroundImage, sunControllerImage)
}
}
}
cause UIImage is Optional .
Swift have a concept of failable initializers and UIImage is have of them. The initializer returns an Optional so if the image cannot be created it will return nil.
Upvotes: 1