質問編集履歴
1
質問の内容の簡略化
    
        title	
    CHANGED
    
    | @@ -1,1 +1,1 @@ | |
| 1 | 
            -
             | 
| 1 | 
            +
            シャッフル処理が分かりません
         | 
    
        body	
    CHANGED
    
    | @@ -1,24 +1,17 @@ | |
| 1 | 
            -
            ### | 
| 1 | 
            +
            ###
         | 
| 2 | 
            -
            5×5の数じのボタンをランダムに配置させたいです。
         | 
| 3 2 |  | 
| 3 | 
            +
             | 
| 4 | 
            -
            ### | 
| 4 | 
            +
            ###
         | 
| 5 | 
            -
            1ランダムに配列内ではなっているのにボタンに反映されない
         | 
| 6 | 
            -
             | 
| 5 | 
            +
            シャッフル関数の意味が分かりません
         | 
| 7 6 | 
             
            ```
         | 
| 8 7 | 
             
            エラーメッセージ
         | 
| 9 8 | 
             
            ```
         | 
| 10 9 |  | 
| 11 10 | 
             
            ###該当のソースコード
         | 
| 12 11 | 
             
            ```swift
         | 
| 13 | 
            -
            ここにご自身が実行したソースコードを書いてください
         | 
| 14 | 
            -
            ```
         | 
| 15 | 
            -
            import UIKit
         | 
| 16 | 
            -
            import Foundation
         | 
| 17 | 
            -
             | 
| 18 | 
            -
            extension Array{
         | 
| 19 | 
            -
             | 
| 12 | 
            +
            mutating func shuffle(){
         | 
| 20 13 | 
             
                for i in 0..<self.count {
         | 
| 21 | 
            -
                  let j = Int(arc4random_uniform(UInt32(self. | 
| 14 | 
            +
                  let j = Int(arc4random_uniform(UInt32(self.count)))
         | 
| 22 15 | 
             
                  if i != j {
         | 
| 23 16 | 
             
                  swap(&self[i], &self[j])
         | 
| 24 17 | 
             
                  }
         | 
| @@ -27,68 +20,13 @@ | |
| 27 20 |  | 
| 28 21 | 
             
              }
         | 
| 29 22 |  | 
| 23 | 
            +
            ```
         | 
| 30 24 |  | 
| 31 | 
            -
              var shuffled:Array {
         | 
| 32 | 
            -
                var copied = Array<Element>(self)
         | 
| 33 | 
            -
                copied.shuffle()
         | 
| 34 | 
            -
                return copied
         | 
| 35 | 
            -
              }
         | 
| 36 | 
            -
            }
         | 
| 37 25 |  | 
| 38 | 
            -
             | 
| 39 | 
            -
            class ViewController: UIViewController {
         | 
| 40 | 
            -
              
         | 
| 41 | 
            -
              var numButtons = [UIButton]()
         | 
| 42 | 
            -
              var newArray:[UIButton] = []
         | 
| 43 | 
            -
              var arrayTitle:[Int] = []
         | 
| 44 | 
            -
             | 
| 45 | 
            -
             override func viewDidLoad() {
         | 
| 46 | 
            -
                
         | 
| 47 | 
            -
                super.viewDidLoad()
         | 
| 48 | 
            -
                
         | 
| 49 | 
            -
                let screenWidth = self.view.bounds.width
         | 
| 50 | 
            -
                let width = screenWidth/5
         | 
| 51 | 
            -
                
         | 
| 52 | 
            -
             | 
| 53 | 
            -
            for i in 0 ..< 5 {
         | 
| 54 | 
            -
                  for j in 0 ..< 5 {
         | 
| 55 26 |  | 
| 56 | 
            -
                    
         | 
| 57 | 
            -
                    let button = UIButton()
         | 
| 58 | 
            -
                    button.frame = CGRect(x: width * CGFloat(i), y: 150 + width * CGFloat(j+1), width: width, height: width)
         | 
| 59 | 
            -
            //        button.tag = i * 5 + j
         | 
| 60 | 
            -
            //        button.setTitle("(tag)", for: .normal)
         | 
| 61 | 
            -
                    button.setTitleColor(UIColor.black, for:.normal)
         | 
| 62 | 
            -
                    button.backgroundColor = UIColor(red: 255/255.0, green: 209/255.0, blue: 160/255.0, alpha: 1.0)
         | 
| 63 | 
            -
            //        let title:String = button.currentTitle!
         | 
| 64 | 
            -
            //        let titleCast:Int = Int(title)!
         | 
| 65 | 
            -
                  
         | 
| 66 | 
            -
                    button.addTarget(self, action: #selector(tappedButton), for:.touchUpInside)
         | 
| 67 | 
            -
                    numButtons.append(button)
         | 
| 68 | 
            -
                    view.addSubview(button)
         | 
| 69 27 |  | 
| 70 | 
            -
                  
         | 
| 71 | 
            -
                    }
         | 
| 72 | 
            -
                    
         | 
| 73 | 
            -
                  
         | 
| 74 | 
            -
                  }
         | 
| 75 | 
            -
                
         | 
| 76 | 
            -
                  for btn in numButtons{
         | 
| 77 | 
            -
                  let rdm = arc4random()
         | 
| 78 | 
            -
                  btn.tag = rdm
         | 
| 79 | 
            -
                  let tag = btn.tag
         | 
| 80 | 
            -
                  btn.setTitle("(tag)", for: .normal)
         | 
| 81 | 
            -
                  }
         | 
| 82 | 
            -
                }
         | 
| 83 28 |  | 
| 84 | 
            -
                    
         | 
| 85 | 
            -
             | 
| 86 | 
            -
             | 
| 87 29 | 
             
            ###試したこと
         | 
| 88 | 
            -
            課題に対してアプローチしたことを記載してください
         | 
| 89 | 
            -
             | 
| 30 | 
            +
            関数jにいれた乱数は重複があるはずなのでiとちがった際に値を交換したとしても、配列に重複が生まれる気がするのですが、どうして重複なく、配列の値をシャッフルできたのか教えていただきたいです。
         | 
| 90 | 
            -
            それがなぜかがわからないです。
         | 
| 91 | 
            -
             | 
| 92 | 
            -
            その後、タイトルをいじろうと思って、乱数を作りタイトルにいれましたが、ランダムな値で配置されるものの、重複した値が並べられてしまいます。
         | 
| 93 31 | 
             
            ###補足情報(言語/FW/ツール等のバージョンなど)
         | 
| 94 32 | 
             
            より詳細な情報
         |