ios 无法将“Int”类型的值转换为预期的参数类型“UInt32”

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/35562635/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-31 08:34:15  来源:igfitidea点击:

Cannot convert value of type 'Int' to expected argument type 'UInt32'

iosswiftintarc4randomuint32

提问by Tom Fox

Any ideas on what I can do to fix this error, I am trying to get ten random numbers so I can then query firebase for a set of questions which contain the random numbers. Screenshot down below. I have also added the code...

关于我可以做些什么来修复这个错误的任何想法,我正在尝试获取十个随机数,以便我可以在 firebase 中查询一组包含随机数的问题。下面截图。我还添加了代码...

import UIKit
import Firebase

class QuestionViewController: UIViewController {

var amountOfQuestions = 2

override func viewDidLoad() {
    super.viewDidLoad()

    // Do any additional setup after loading the view.
}

override func viewDidAppear(animated: Bool) {
    super.viewDidAppear(true)

    //Use a for loop to get 10 questions
    for _ in 1...10{
        //generate a random number between 1 and the amount of questions you have
        var randomNumber = Int(arc4random_uniform(amountOfQuestions - 1)) + 1

        //The reference to your questions in firebase (this is an example from firebase itself)
        let ref = Firebase(url: "https://dinosaur-facts.firebaseio.com/dinosaurs")
        //Order the questions on their value and get the one that has the random value
        ref.queryOrderedByChild("value").queryEqualToValue(randomNumber)
            .observeEventType(.ChildAdded, withBlock: {
                snapshot in
                //Do something with the question
                println(snapshot.key)
            })
    }    }



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

@IBAction func truepressed(sender: AnyObject) {
}

@IBAction func falsePressed(sender: AnyObject) {
}

enter image description here

在此处输入图片说明

采纳答案by JAL

Make your amountOfQuestionsvariable an UInt32rather than an Intinferred by the compiler.

使您的amountOfQuestions变量成为UInt32而不是Int编译器推断的变量。

var amountOfQuestions: UInt32 = 2

// ...

var randomNumber = Int(arc4random_uniform(amountOfQuestions - 1)) + 1

arc4random_uniformrequires a UInt32.

arc4random_uniform需要一个UInt32.

From the Darwin docs:

达尔文文档

arc4random_uniform(u_int32_t upper_bound);

回答by yesthisisjoe

Declare amountOfQuestions as a UInt32:

将 amountOfQuestions 声明为 UInt32:

var amountOfQuestions: UInt32 = 2

PS: If you want to be grammatically correct it's numberof questions.

PS:如果你想在语法上正确,那就是问题的数量

回答by gaskbr

First thing: The method "arc4random_uniform" expects an argument of type UInt32, so when you put that subtraction there, it converted the '1' you wrote to UInt32.

第一件事:方法“arc4random_uniform”需要一个 UInt32 类型的参数,所以当你把减法放在那里时,它会将你写的“1”转换为 UInt32。

Second thing: In swift you can't subtract a UInt32 (the '1' in your formula) from an Int (in this case 'amountOfQuestions').

第二件事:在 swift 中,您不能从 Int(在本例中为“amountOfQuestions”)中减去 UInt32(公式中的“1”)。

To solve it all, you'll have to consider changing the declaration of 'amountOfQuestions' to:

要解决这一切,您必须考虑将 'amountOfQuestions' 的声明更改为:

var amountOfQuestions = UInt32(2)

That should do the trick :)

这应该够了吧 :)