ios - Swift - 即使我插入了不同的数字,文本字段也会返回 0

标签 ios swift text random

我是 Swift 新手,我正在尝试制作一个小应用程序,给定两个值 (a,b),返回两个数字之间的随机数 (a < randomnumber < b)

用户必须在两个不同的文本字段中写入 a 和 b 的值,然后只需按一个按钮即可

问题是: 我告诉 NSLog 在生成随机数之前显示这两个值,因为我注意到有些东西没有按预期工作,结果显然是我插入的第二个值,无论是 a 还是 b,对于某些(未知)原因,等于0!例如,如果我在 a-textfield 中写入值 10,然后在 b-textfield 中插入值 40,则 NSLog 显示 a = 10, b = 0。如果我之前插入 b 值,则 NSLog 显示 b = 40,a = 0。 这些是代码行:

@IBAction func TextAAction(sender: AnyObject) {
    a = UInt32(TextA.text!)!

}


@IBAction func TextBAction(sender: AnyObject) {
    b = UInt32(TextB.text!)!

}


@IBAction func randomInBetweenAction(sender: AnyObject) {
    NSLog("a=%ld, b=%ld", a, b)
    randomInBetween()
    unHide()
    Label.text = "\(randomNumberInBetween)"

}

如您所见,值 a 等于在 TextA 字段中插入的值,值 b 等于在 TextB 字段中插入的值

randomInBetween() 是在 a 和 b 之间生成随机数的函数,它确实有效。问题是,由于某种原因,其中一个值(我插入的第二个值)始终为 0,即使我将其设置为不同的数字!

关于为什么会发生这种情况以及如何解决它有什么想法吗?

@ReinerMelian 这是完整的代码

import UIKit

class ViewController: UIViewController {


@IBOutlet weak var Label: UILabel!
@IBOutlet weak var Button: UIButton!
@IBOutlet weak var InvariableLabel: UILabel!
@IBOutlet weak var TextA: UITextField!
@IBOutlet weak var TextB: UITextField!
@IBOutlet weak var AndLabel: UILabel!
@IBOutlet weak var Button2: UIButton!

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

    Hide()

}

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

var randomNumber = Int()


func randomNUMBER() -> Int {
    randomNumber = Int(arc4random())
    return randomNumber
}

func Hide() {
    Label.hidden = true
}

func unHide() {
    Label.hidden = false
}

var a = UInt32()
var b = UInt32()

var randomNumberInBetween = Int()

func randomInBetween() -> Int {
    if b>a {
        randomNumberInBetween = Int(arc4random_uniform(b - a) + a)
         return randomNumberInBetween

    }  else {
randomNumberInBetween = Int(arc4random_uniform(a - b) + a)
return randomNumberInBetween
}


}



@IBAction func ButtonAction(sender: AnyObject) {
    randomNUMBER()
    unHide()
    Label.text = "\(randomNumber)"
    NSLog("\(randomNumber)")
}


@IBAction func TextAAction(sender: AnyObject) {
    a = UInt32(TextA.text!)!


}


@IBAction func TextBAction(sender: AnyObject) {
    b = UInt32(TextB.text!)!

}


@IBAction func randomInBetweenAction(sender: AnyObject) {
    NSLog("a=%ld, b=%ld", a, b)
    randomInBetween()
    NSLog("\(randomInBetween)")
    unHide()
    Label.text = "\(randomNumberInBetween)"
    NSLog("\(randomInBetween)")
}



}

(PS:正如你所看到的,还有一个函数可以生成一个没有任何约束的随机数,但这部分工作得很好..问题出在代码的另一部分,即返回的部分两个给定值 (a,b) 之间的随机数

最佳答案

确保您的 socket (TextATextB) 和 IBAction (randomInBetweenAction) 连接正确,删除

@IBAction func TextAAction(sender: AnyObject) {
    a = UInt32(TextA.text!)!

}

@IBAction func TextBAction(sender: AnyObject) {
    b = UInt32(TextB.text!)!

}

并在点击按钮时尝试此功能:

@IBAction func randomInBetweenAction(sender: AnyObject) {
    a = UInt32(TextA.text!)!
    b = UInt32(TextB.text!)!
    NSLog("a=%ld, b=%ld", a, b)
    randomInBetween()
    NSLog("\(randomInBetween)")
    unHide()
    Label.text = "\(randomNumberInBetween)"
    NSLog("\(randomInBetween)")
}

关于ios - Swift - 即使我插入了不同的数字,文本字段也会返回 0,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/37930011/

相关文章:

ios - VTDecompressionSessionDecodeFrame 返回 imageBuffer = nil 但 OSStatus = noErr

ios - 附加在函数内部的数组在其他类中不起作用

javascript - 是否有任何库可以有效地编辑 Javascript 中的大字符串?

javascript - 如何在snap.svg中使用textPath作为文本?

ios - 错误二元运算符 '==' 不能应用于两个 'MKAnnotation' 操作数

ios - Swift 5 UIButton 标题没有改变

android - 在 sqflite 数据库创建时插入记录

ios - 解码JSON并将其映射到现有对象Swift

swift - 如何基于分层数据创建 Swift 序列?

python - Pandas 替换(删除)字符串中的不同字符