本文介绍了Apple Swift - 将变量放在arc4random_uniform中的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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



我的目标:为了能够使用一个变量

问题:我不知道如何在createBalloons()函数之外移动balloonCount。



解决方案?在currentIndex之上定义balloonCount,程序将识别此变量。 (我被告知这是解决方案,我只是不幸的是不知道如何做这个....)



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

  // 
//

import UIKit

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()
//加载视图之后进行任何其他设置,通常来自nib。
self.createBalloons()
}

override func didReceiveMemoryWarning(){
super.didReceiveMemoryWarning()
//处理任何可以重新创建。
}

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

'ViewController'没有名为'currentIndex'的成员

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

currentIndex = currentIndex + 1
pre>

'ViewController'没有名为'currentIndex'的成员

  if currentIndex> 99 {

'ViewController'没有名为'currentIndex'的成员
currentIndex = 50
}

 } 


func createBalloons(){
为var balloonCount = 0; balloonCount
var balloon = Balloon()


//我不认为你需要一个开关。
let randomNumber = Int(arc4random_uniform(UInt32(4)))

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

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

self.balloons.append(balloon)

}
}

}


解决方案

您可以为使用另一个实例变量的变量分配一个函数。



我会使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]
...


Got a rather basic Swift question for you all.

My goal: To be able to use a variable (count of items in an array) inside of arc4random.

Problem: I don't know how to move "balloonCount" outside of the createBalloons() function.

Solution? Define "balloonCount" above currentIndex, that way the program will recognize this variable. (I was told this is the solution, I just unfortunately do not know how to do this.... yet)

Here is my (updated) swift code:

//
//

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' does not have a member named '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' does not have a member named 'currentIndex'

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

    currentIndex = currentIndex + 1

'ViewController' does not have a member named 'currentIndex'

    if currentIndex > 99 {

'ViewController' does not have a member named 'currentIndex' 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)

    }
}

}
解决方案

I don't think you can assign a function to a variable that uses another instance variable.

I would make currentIndex an Int and then create a seperate getRandomIndex function. So the code would look something like this:

var currentIndex: Int!
func getRandomIndex() -> Int {
    return Int(arc4random_uniform(UInt32(balloons.count)))
}

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

这篇关于Apple Swift - 将变量放在arc4random_uniform中的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

10-10 04:04