图像文字数组在 swift 中得到 "Type of expression is ambiguous without more context" 5
array of image literals getting "Type of expression is ambiguous without more context" in swift 5
我是编码新手,正在努力学习 Swift。我正在制作一个简单的“石头剪刀布”应用程序来练习使用 MVC。
我有一个数组(让 imagesArray = [图像文字,图像文字,图像文字]
当我在控制器中有图像数组时,它工作正常,但是当我尝试将它移动到模型时,我收到“表达式类型不明确,没有更多上下文”错误。模型中不允许使用图像阵列吗?我的理解是数据应该保存在模型中,这就是为什么我试图把它放在那里。
任何想法将不胜感激:)
struct GameBrain {
let images = [ #imageLiteral(resourceName: "rock"), #imageLiteral(resourceName: "paper"), #imageLiteral(resourceName: "scissors")]
func playGame() -> Int {
let choices = [0,1,2]
let choice = choices.randomElement()
return choice!
}
mutating func getWinner(choice: Int?) {
}
}
class ViewController: UIViewController {
var gameBrain = GameBrain()
@IBOutlet weak var imageViewLeft: UIImageView!
@IBOutlet weak var imageViewRight: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func goButton(_ sender: UIButton) {
imageViewLeft.image = images[gameBrain.playGame()]
imageViewRight.image = images[gameBrain.playGame()]
}
}
如果将 images
移动到模型中,则必须调整引用
@IBAction func goButton(_ sender: UIButton) {
imageViewLeft.image = gameBrain.images[gameBrain.playGame()]
imageViewRight.image = gameBrain.images[gameBrain.playGame()]
}
显然不需要索引,所以这更简单
var playGame : UIImage {
return images.randomElement()!
}
和
@IBAction func goButton(_ sender: UIButton) {
imageViewLeft.image = gameBrain.playGame
imageViewRight.image = gameBrain.playGame
}
我是编码新手,正在努力学习 Swift。我正在制作一个简单的“石头剪刀布”应用程序来练习使用 MVC。
我有一个数组(让 imagesArray = [图像文字,图像文字,图像文字]
当我在控制器中有图像数组时,它工作正常,但是当我尝试将它移动到模型时,我收到“表达式类型不明确,没有更多上下文”错误。模型中不允许使用图像阵列吗?我的理解是数据应该保存在模型中,这就是为什么我试图把它放在那里。
任何想法将不胜感激:)
struct GameBrain {
let images = [ #imageLiteral(resourceName: "rock"), #imageLiteral(resourceName: "paper"), #imageLiteral(resourceName: "scissors")]
func playGame() -> Int {
let choices = [0,1,2]
let choice = choices.randomElement()
return choice!
}
mutating func getWinner(choice: Int?) {
}
}
class ViewController: UIViewController {
var gameBrain = GameBrain()
@IBOutlet weak var imageViewLeft: UIImageView!
@IBOutlet weak var imageViewRight: UIImageView!
override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view.
}
@IBAction func goButton(_ sender: UIButton) {
imageViewLeft.image = images[gameBrain.playGame()]
imageViewRight.image = images[gameBrain.playGame()]
}
}
如果将 images
移动到模型中,则必须调整引用
@IBAction func goButton(_ sender: UIButton) {
imageViewLeft.image = gameBrain.images[gameBrain.playGame()]
imageViewRight.image = gameBrain.images[gameBrain.playGame()]
}
显然不需要索引,所以这更简单
var playGame : UIImage {
return images.randomElement()!
}
和
@IBAction func goButton(_ sender: UIButton) {
imageViewLeft.image = gameBrain.playGame
imageViewRight.image = gameBrain.playGame
}