我有一个类,这是我的模型,用于获取文本值和要显示的图像。
我试图将文本值和图像返回到控制器以在UILabel和ImageView上显示,但由于某些原因,我的函数不断生成错误,例如未指定返回类型或“初始化前使用了变量'image'”
有人可以告诉我编写此功能的最佳方法是什么吗?这两个数组将是固定的,并且将始终至少具有一个值。 我也想知道如果数组可能为nil的话会怎样。
struct LoginCardSwipe{
let loginText = ["Discover new and interesting people near by" , "Swipe Right to meet new" , "Hey how are you?", "Discover new and interesting people near by" , "Discover new ", " Heoosadda" ]
let loginImage = ["tinder1", "tinder2", "tinder1", "tinder2", "tinder2"]
var currentIndex = 0
mutating func getCardValueRight() -> String{
currentIndex += 1
if currentIndex >= 0, currentIndex < loginText.count {
let textValue = loginText[currentIndex]
return textValue
} else{
return ""
}
}
mutating func getCardImageRight() -> UIImage{
currentIndex += 1
var image: UIImage?
if currentIndex >= 0, currentIndex < loginText.count {
let cardImage = loginImage[currentIndex]
image = UIImage(named: cardImage)
}
return image!
}
mutating func getCardValueLeft() -> String{
currentIndex -= 1
var textValue: String
if currentIndex >= 0, currentIndex < loginText.count {
textValue = loginText[currentIndex]
return textValue
}else{
return ""
}
}
mutating func getCardImageLeft() -> UIImage{
currentIndex -= 1
var image: UIImage
if currentIndex >= 0, currentIndex < loginText.count {
let cardImage = loginImage[currentIndex]
image = UIImage(named: cardImage)!
}
return image
}
}
答案 0 :(得分:0)
两种左右图像方法的正确实现
mutating func getCardImageRight() -> UIImage? {
currentIndex += 1
if currentIndex >= 0, currentIndex < loginText.count {
let cardImage = loginImage[currentIndex]
return UIImage(named: cardImage)
}
return nil
}
//
mutating func getCardImageLeft() -> UIImage? {
currentIndex -= 1
if currentIndex >= 0, currentIndex < loginText.count {
let cardImage = loginImage[currentIndex]
return UIImage(named: cardImage)
}
return nil
}
//
以及使用时
if let left = strInstance.getCardImageLeft() {
}
强行使用-解开可能为nil的值将导致崩溃