【问题标题】:Check the value of a constant in Swift在 Swift 中检查常量的值
【发布时间】:2016-05-08 20:42:47
【问题描述】:

所以我正在编写我的第一个应用程序,是的,从头开始,我以前从未做过类似的事情,所以请多多包涵。
我想获取第一个常量的随机值,并使用它来通过视图控制器上的标签确定屏幕上显示的内容,这对某些人来说可能很容易,但我在这里真的很挣扎。我注释掉了我的代码,所以你知道我打算做什么。现在,我知道我可以采用许多不同的方式,例如根本没有标签和图像上的 Photoshop 短语,但不……我想要 CODE!

有什么想法吗?非常感谢大家:3

import UIKit

class ViewController: UIViewController {
    let random = Int(arc4random_uniform(11)) //Randomised int values from 0 to 11 (-1)
    @IBOutlet weak var text: UILabel!
    @IBOutlet weak var phrase: UIButton! //this button should reset the entire process over again
    @IBOutlet var imageauthor: UIImageView!
    override func viewDidLoad() {
            super.viewDidLoad()
        self .imageauthor.image = UIImage(named:"0\(random).jpg") //Viewcontroller displays a random image out of randomised value
        self .text.text = ("''") //this should somehow check what the randomised value is and call the Uilabel text bellow it
    }


    var string1 = ("My fist app has many holes1")
    ... string2 = ("My fist app has many holes2")
    ... string3.... 

【问题讨论】:

  • 我不确定你想要什么。您想根据随机值在标签中附加一些文本吗?例如:随机值 = 1,所以标签文本 = string1 ?
  • 是的!我的第一个常量 let random = Int(arc4random_uniform(11)) 将从 0-10(11-1=10) 中随机化一个数字,这将确定我的 image.assets 中的图像编号,下一步是显示给定的字符串与图像相关的给定标签。明白了吗?
  • 好的,所以@appzYourLife 的回答应该可以完成这项工作。
  • 我正在尝试实现他的方法,但仍然......你知道......我真的不知道该怎么做,我有线索但你知道......如果这是然后我需要什么,我会继续尝试和研究语法,直到我得到它的工作,谢谢。
  • 你实现了吗?

标签: swift random constants let


【解决方案1】:
  1. 您将句子放入一个数组中(我们称之为sentences)。
  2. 您在0sentences.count 之间生成一个随机值(不包括在内)。
  3. 您使用随机整数从sentences 数组中选择一个值。

像这样:

let sentences = ["My fist app has many holes1", "My fist app has many holes2", "My fist app has many holes3"];
let random = Int(arc4random_uniform(UInt32(sentences.count)))
let randomSentence = sentences[random]

【讨论】:

  • @SamuelPantoja:我的回答中解释了每一行。
【解决方案2】:

具体应该是这样的:

import UIKit

class ViewController: UIViewController {


    @IBOutlet weak var imageauthor: UIImageView!
    @IBOutlet weak var text: UILabel!
    // 1
    @IBAction func showRandom(sender: UIButton) {
        showRandom()
    }

    // 2
    let arrayOfStrings: [String] = ["String of image 0", "String of image 1", "String of image 2", "String of image 3", "String of image 4", "String of image 5", "String of image 6", "String of image 7", "String of image 8", "String of image 9"]

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

        //3
        showRandom()
    }

    //4
    func showRandom() {
        let random = Int(arc4random_uniform(UInt32(arrayOfStrings.count)))

        self.imageauthor.image = UIImage(named:"0\(random).jpg")
        self.text.text = (arrayOfStrings[random])
    }

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


}


//1 在您的按钮上创建连接Action:与Outlet 的过程相同,但请选择Connection : ActionType : UIButton。并在点击时执行你的随机函数。 See documentation

//2 声明一个包含所有句子的数组。尊重秩序。

//3 在 vi​​ewDidLoad 上执行你的随机函数

//4 创建函数showRandom() 将随机图像和相应的文本放入您的imageView 和标签中。

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2018-09-28
    • 1970-01-01
    • 2014-11-29
    相关资源
    最近更新 更多