gpt4 book ai didi

class - Apple Swift - 在 arc4random_uniform 中放置一个变量

转载 作者:行者123 更新时间:2023-11-28 11:22:15 25 4
gpt4 key购买 nike

有一个相当基本的 Swift 问题要问你们。

我的目标:能够在 arc4random 中使用变量(数组中的项目数)。

问题:我不知道如何将“balloonCount”移到 createBalloons() 函数之外。

解决方案? 在 currentIndex 之上定义“balloonCount”,这样程序就会识别这个变量。 (我被告知这是解决方案,不幸的是我还不知道该怎么做......)

这是我的(更新的)swift 代码:

//
//

import UIKit

class ViewController: UIViewController {
@IBOutlet weak var balloonsLabel: UILabel!
@IBOutlet weak var backgroundImageView: UIImageView!


var balloons:[Balloon] = []

var currentIndex = Int(arc4random_uniform(UInt32(balloons.count)))

“ViewController.type”没有名为“balloons”的成员

override func viewDidLoad() {
super.viewDidLoad()
// Do any additional setup after loading the view, typically from a nib.
self.createBalloons()
}

override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}

@IBAction func nextBalloonBarButtonItemPressed(sender: UIBarButtonItem) {
let balloon = balloons[currentIndex]

“ViewController”没有名为“currentIndex”的成员

    balloonsLabel.text = "\(balloon.number) Balloon"
backgroundImageView.image = balloon.image

currentIndex = currentIndex + 1

“ViewController”没有名为“currentIndex”的成员

    if currentIndex > 99 {

“ViewController”没有名为“currentIndex”的成员 当前索引 = 50

}


func createBalloons () {
for var balloonCount = 0; balloonCount <= 99; ++balloonCount{

var balloon = Balloon()


//I dont think you need a switch here.
let randomNumber = Int(arc4random_uniform(UInt32(4)))

let balloonName:String = "redBalloon\(randomNumber).jpg"

balloon.image = UIImage(named: balloonName)
balloon.number = balloonCount

self.balloons.append(balloon)

}
}

}

最佳答案

试试这个,但不确定语法是否是 100%。我在这个 comp 上没有 Xcode。让我知道这是否有帮助。另外,如果有人在这里看到任何似乎不对的地方,请在下面发表评论,我会更新我的答案。

试试这个:

var currentIndex = Int(arc4random_uniform(UInt32(balloons.count)))

另外我在这里做了一些修改:

func createBalloons () {
for var balloonCount = 0; balloonCount <= 99; ++balloonCount{

var balloon = Balloon()


//I dont think you need a switch here.
let randomNumber = Int(arc4random_uniform(UInt32(4)))

let balloonName:String = "redBalloon\(randomNumber).jpg"

balloon.image = UIImage(named: balloonName)
balloon.number = balloonCount

self.balloons.append(balloon)

}
}

关于class - Apple Swift - 在 arc4random_uniform 中放置一个变量,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26770730/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com