检查 Swift 中常量的值
Check the value of a constant in Swift
所以我正在编写我的第一个应用程序,是的,从头开始,我以前从未做过任何类似的事情,所以请多多包涵。
我想获取第一个常量的随机值,并使用它来确定通过视图控制器上的标签显示在屏幕上的内容,这对某些人来说可能很容易,但我在这里真的很挣扎。我注释掉了我的代码,所以你知道我打算它做什么。现在,我知道我可以采用多种不同的方式,例如根本没有标签和图像上的 photoshop 短语但是不......我想要代码!
有什么想法吗?非常感谢大家 :3 <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....
- 你把你的句子放在一个数组中(我们称之为
sentences
)。
- 您生成一个介于
0
和 sentences.count
之间的随机值(不包括在内)。
- 您使用随机整数从
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]
具体应该是这样的:
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
相同的过程但是 select Connection : Action
和 Type : UIButton
代替。并在点击时执行你的随机函数。 See documentation
//2 声明一个包含所有句子的数组。尊重秩序。
//3 在 viewDidLoad 上执行你的随机函数
//4 创建函数 showRandom()
将随机图像和相应的文本放入您的 imageView 和标签中。
所以我正在编写我的第一个应用程序,是的,从头开始,我以前从未做过任何类似的事情,所以请多多包涵。
我想获取第一个常量的随机值,并使用它来确定通过视图控制器上的标签显示在屏幕上的内容,这对某些人来说可能很容易,但我在这里真的很挣扎。我注释掉了我的代码,所以你知道我打算它做什么。现在,我知道我可以采用多种不同的方式,例如根本没有标签和图像上的 photoshop 短语但是不......我想要代码!
有什么想法吗?非常感谢大家 :3 <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....
- 你把你的句子放在一个数组中(我们称之为
sentences
)。 - 您生成一个介于
0
和sentences.count
之间的随机值(不包括在内)。 - 您使用随机整数从
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]
具体应该是这样的:
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
相同的过程但是 select Connection : Action
和 Type : UIButton
代替。并在点击时执行你的随机函数。 See documentation
//2 声明一个包含所有句子的数组。尊重秩序。
//3 在 viewDidLoad 上执行你的随机函数
//4 创建函数 showRandom()
将随机图像和相应的文本放入您的 imageView 和标签中。