Home>

Programming is a beginner. It ’s also the first time I ’ve used such a question site,
I am sorry if there is a lack of questions.

Ipad app that displays pictures randomly in Xcode
I was making it. When executed, Build Succeeded will be displayed, but only hoge1 or hoge3 will be displayed among images hoge1 ~ hoge10.

The questions are as follows.
1 In the first place, arc4random ()% 10 can't be used with swift4?
1.1 Despite the above, random numbers can be generated.
1.2 Trying to use let num = random ()% 10 instead of arc4random ()% 10
'random ()'is unavailable in Swift: Use arc4random instead.

2 It seems that every time the button is pressed, an integer from 0 to 9 is generated, but if the random integer is 1, the image of hoge3 appears and if it is 2-9, the image of hoge1 Get out.

Error message
Error message
There is no error message, but hoge2, hoge4 ~ hoge10 is not displayed.
`
### Applicable source code
swift
import UIKit
class ViewController: UIViewController {

    @IBOutlet weak var image1: UIImageView!
    var img1 = UIImage (named: "hoge1")!
    var img2 = UIImage (named: "hoge2")!
    var img3 = UIImage (named: "hoge3")!
    var img4 = UIImage (named: "hoge4")!
    var img5 = UIImage (named: "hoge5")!
    var img6 = UIImage (named: "hoge6")!
    var img7 = UIImage (named: "hoge7")!
    var img8 = UIImage (named: "hoge8")!
    var img9 = UIImage (named: "hoge9")!
    var img10 = UIImage (named: "hoge10")!
    override func viewDidLoad () {
        super.viewDidLoad ()
        // Do any additional setup after loading the view, typically from a nib.
    }
    @IBAction func button2 (_ sender: Any) {
        let random = arc4random ()% 10
        print (random)
        if (random == 0) {
            image1.image = img10
        }
        if (random == 9) {
            image1.image = img9
        }
        if (random == 8) {
            image1.image = img8
        }
        if (random == 7) {
            image1.image = img7
        }
        if (random == 6) {
            image1.image = img6
        }
        if (random == 5) {
            image1.image = img4
        }
        if (random == 4) {
            image1.image = img4
        }
        if (random == 3) {
            image1.image = img3
        }
        if (random == 2) {
            image1.image = img2
        }
        if (random == 1) {
            image1.image = img1
        }
       else {
            image1.image = img3
        }
    }
    @IBOutlet weak var button2: UIButton!
    @IBAction func button (_ sender: UIButton) {
    }
    override func didReceiveMemoryWarning () {
        super.didReceiveMemoryWarning ()
        // Dispose of any resources that can be recreated.
    }
}
Tried

Use let num = random ()% 10 instead of arc4random ()% 10

Supplemental information (FW/tool version etc.)

mac OS HighSieera 10.13.6
Version 9.4.1 (9F2000)
Swift version 4.1.2

By the way, I made it by looking at the following site.
https://qiita.com/egplnt/items/36b530c46b61345ea180#%E5%8F%82%E8%80%83%E6%8A%BC%E3%81%99%E3%81%9F%E3%81%B3 % E3% 81% AB% E7% 95% B0% E3% 81% AA% E3% 82% 8B% E5% 86% 99% E7% 9C% 9F% E3% 82% 92% E8% A1% A8% E7 % A4% BA% E3% 81% 99% E3% 82% 8B

  • Answer # 1

    Because it is not connected with else if, execute if blocks in order, and finally

    if (random == 1) {
                image1.image = img1
            }
           else {
                image1.image = img3
            }

    . If it is not 1, it will be img3.

Related articles