Class Apple Swift-在arc4random_制服中放置一个变量
我有一个很简单的问题要问你们 我的目标:能够在arc4random中使用变量(数组中的项目计数) 问题:我不知道如何将“BalloodCount”移到CreateBloods()函数之外 解决方案?在currentIndex上方定义“BalloodCount”,这样程序将识别此变量。(有人告诉我这就是解决方案,但不幸的是,我还不知道如何做到这一点……) 以下是我的(更新的)swift代码:Class Apple Swift-在arc4random_制服中放置一个变量,class,variables,swift,Class,Variables,Swift,我有一个很简单的问题要问你们 我的目标:能够在arc4random中使用变量(数组中的项目计数) 问题:我不知道如何将“BalloodCount”移到CreateBloods()函数之外 解决方案?在currentIndex上方定义“BalloodCount”,这样程序将识别此变量。(有人告诉我这就是解决方案,但不幸的是,我还不知道如何做到这一点……) 以下是我的(更新的)swift代码: // // import UIKit class ViewController: UIViewCont
//
//
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
if currentIndex > 99 {
“ViewController”没有名为“currentIndex”的成员
balloonsLabel.text = "\(balloon.number) Balloon"
backgroundImageView.image = balloon.image
currentIndex = currentIndex + 1
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)
}
}
}
}
func创建引出序号(){
对于var BallookCount=0;BallookCount尝试此方法,但不确定语法是否为100%。我在该组件上没有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)
}
}
func创建引出序号(){
对于var BallookCount=0;BallookCount尝试此方法,但不确定语法是否为100%。我在该组件上没有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)
}
}
func创建引出序号(){
对于var BallookCount=0;BallookCount尝试此方法,但不确定语法是否为100%。我在该组件上没有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)
}
}
func创建引出序号(){
对于var BallookCount=0;BallookCount尝试此方法,但不确定语法是否为100%。我在该组件上没有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)
}
}
func创建引出序号(){
对于var-ballocount=0;ballocount我认为不能将函数分配给使用另一个实例变量的变量
我将currentIndex设置为Int,然后创建一个单独的getRandomIndex函数。因此代码如下所示:
var currentIndex: Int!
func getRandomIndex() -> Int {
return Int(arc4random_uniform(UInt32(balloons.count)))
}
@IBAction func nextBalloonBarButtonItemPressed(sender: UIBarButtonItem) {
currentIndex = getRandomIndex()
let balloon = balloons[currentIndex]
...
我认为不能将函数分配给使用另一个实例变量的变量
我将currentIndex设置为Int,然后创建一个单独的getRandomIndex函数。因此代码如下所示:
var currentIndex: Int!
func getRandomIndex() -> Int {
return Int(arc4random_uniform(UInt32(balloons.count)))
}
@IBAction func nextBalloonBarButtonItemPressed(sender: UIBarButtonItem) {
currentIndex = getRandomIndex()
let balloon = balloons[currentIndex]
...
我认为不能将函数分配给使用另一个实例变量的变量
我将currentIndex设置为Int,然后创建一个单独的getRandomIndex函数。因此代码如下所示:
var currentIndex: Int!
func getRandomIndex() -> Int {
return Int(arc4random_uniform(UInt32(balloons.count)))
}
@IBAction func nextBalloonBarButtonItemPressed(sender: UIBarButtonItem) {
currentIndex = getRandomIndex()
let balloon = balloons[currentIndex]
...
我认为不能将函数分配给使用另一个实例变量的变量
我将currentIndex设置为Int,然后创建一个单独的getRandomIndex函数。因此代码如下所示:
var currentIndex: Int!
func getRandomIndex() -> Int {
return Int(arc4random_uniform(UInt32(balloons.count)))
}
@IBAction func nextBalloonBarButtonItemPressed(sender: UIBarButtonItem) {
currentIndex = getRandomIndex()
let balloon = balloons[currentIndex]
...
我想你要找的是count
属性内置在Array
类型中。在arc4random()中尝试self.baloons.count
)
@JMFR-这似乎不起作用。我还尝试了self.balloonCount.count我想你要找的是count
数组中内置的count
属性。在arc4random()中尝试self.baloons.count
)
@JMFR-这似乎不起作用。我还尝试了self.balloonCount.count我想你要找的是count
数组中内置的count
属性。在arc4random()中尝试self.baloons.count
)
@JMFR-这似乎不起作用。我还尝试了self.balloonCount.count我想你要找的是count
数组中内置的count
属性。在arc4random()中尝试self.baloons.count
)
@JMFR-这似乎不管用。我还尝试了self.balloodcount。count@Joe这个答案对您有帮助吗?谢谢您的帮助!不幸的是,它仍然不起作用。我收到一些错误。请参见下面!var currentIndex=Int(arc4random_uniform(UInt32(balloon.count))(!使用未解析的标识符“balloon”)let balloon=ballods[currentIndex]currentIndex=currentIndex+1^这两行都给了我:“ViewController”没有名为“currentIndex”的成员,应该是ballos。计数以匹配您的数组。@Joe您觉得我的答案有用吗?如果您仍然卡住了,请告诉我。@the_pantless_coder-仍然没有运气:(我已经用你提供的更新代码编辑了我的第一篇文章)还有我遇到的所有错误。我觉得这很简单,我忘记了/错过了,只是不知道是什么。@Joe这个答案帮了你吗?谢谢你帮忙!不幸的是,它仍然不起作用。我遇到了一些错误。请看下面!var currentIndex=Int(arc4random_uniform(UInt32(balloon.count)))(!使用未解析的标识符“balloon”)让balloon=ballos[currentIndex]currentIndex=currentIndex+1^这两行都告诉我:“ViewController”没有名为“currentIndex”的成员,应该是气球。计数以匹配你的数组。@Joe你觉得我的答案有用吗?如果你仍然卡住了,请告诉我。@the_pantless_编码器-仍然没有运气:(我用更新的代码编辑了我的第一篇帖子。)(你提供的)以及我遇到的所有错误。我觉得这很简单,我忘记了/错过了,只是不知道是什么…@Joe这个答案帮你解决了吗?谢谢