質問をすることでしか得られない、回答やアドバイスがある。

15分調べてもわからないことは、質問しよう!

新規登録して質問してみよう
ただいま回答率
85.50%
Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Swift 2

Swift 2は、Apple社が独自に開発を行っている言語「Swift」のアップグレード版です。iOSやOS X、さらにLinuxにも対応可能です。また、throws-catchベースのエラーハンドリングが追加されています。

Q&A

解決済

1回答

1428閲覧

Use stride(from:to:by:) Swift 2から4への書き直し中に発生したエラー

naokun

総合スコア13

Swift

Swiftは、アップルのiOSおよびOS Xのためのプログラミング言語で、Objective-CやObjective-C++と共存することが意図されています

Swift 2

Swift 2は、Apple社が独自に開発を行っている言語「Swift」のアップグレード版です。iOSやOS X、さらにLinuxにも対応可能です。また、throws-catchベースのエラーハンドリングが追加されています。

0グッド

0クリップ

投稿2018/03/08 04:55

編集2018/03/08 05:37

現在、Swift 2の時に書いたパズルゲームのコードをSwift 4に書き直しているのですが、
'stride(to:by:)' is unavailable: Use stride(from:to:by:) free function instead
というエラーがstrideを使っている箇所で出てしまいます。
どうすれば解決するでしょうか?

Swift

1 2import UIKit 3 4class ViewController: UIViewController { 5 6 let BTN_START = 0 //スタート 7 let SCREEN = UIScreen.main.bounds.size //画面サイズ 8 9 //変数 10 var _gameView: UIView! //ゲームビュー 11 var _titleLabel: UILabel! //タイトルラベル 12 var _piece = [UIImageView]() //ピース画像(2) 13 var _data = [Int]() //ピース配置情報(2) 14 var _shuffle: Int = 0 //シャッフル 15 var _startButton: UIButton! //スタートボタン 16 17 18 //==================== 19 //UI 20 //==================== 21 //ロード完了時に呼ばれる 22 override func viewDidLoad() { 23 super.viewDidLoad() 24 25 //ゲーム画面のXY座標とスケールの指定(1) 26 let vx: CGFloat = (SCREEN.width-360)/2 27 let vy: CGFloat = (SCREEN.height-640)/2 28 let scale = SCREEN.width/360 29 _gameView = UIView() 30 _gameView.frame = CGRect(x: vx, y: vy, width: 360, height: 640) 31 _gameView.transform = CGAffineTransform(scaleX: scale, y: scale) 32 self.view.addSubview(_gameView) 33 34 //背景の生成 35 let bg = makeImageView(frame: CGRect(x: 0, y: 0, width: 360, height: 640), 36 image: UIImage(named: "pzl0.png")!) 37 _gameView.addSubview(bg) 38 39 //絵の背景の生成 40 let picturebg = makeImageView(frame: CGRect(x: 29, y: 179, width: 302, height: 302), 41 image: UIImage(named: "pzl2.png")!) 42 _gameView.addSubview(picturebg) 43 44 //タイトルラベルの生成 45 _titleLabel = makeLabel(frame: CGRect(x: 0, y: 90, width: 360, height: 70), 46 text: "15 Puzzle", font: UIFont.systemFont(ofSize: 48)) 47 _gameView.addSubview(_titleLabel) 48 49 //絵のビットマップの取得 50 let picture = UIImage(named: "pzl1.png")! 51 let piece = UIImage(named: "pzl3.png")! 52 for i in 0..<16 { 53 _piece.append(makePieceImageView(frame: CGRect( 54 x: CGFloat(30+(i%4)*75), 55 y: CGFloat(180+Int(i/4)*75), 56 width: 75, height: 75), 57 index: i, picture: picture, piece: piece)) 58 _data.append(i) 59 _gameView.addSubview(_piece[i]) 60 } 61 62 //スタートボタンの生成 63 _startButton = makeButton(frame: CGRect(x: 124, y: 455, width: 114, height: 114), 64 image: UIImage(named: "pzl4.png")!, tag: BTN_START) 65 _gameView.addSubview(_startButton) 66 } 67 68 //ラベルの生成 69 func makeLabel(frame: CGRect, text: String, font: UIFont) -> UILabel { 70 let label = UILabel() 71 label.frame = frame 72 label.text = text 73 label.font = font 74 label.textColor = UIColor.white 75 label.textAlignment = NSTextAlignment.center 76 label.lineBreakMode = NSLineBreakMode.byWordWrapping 77 label.numberOfLines = 0 78 return label 79 } 80 81 //イメージビューの生成 82 func makeImageView(frame: CGRect, image: UIImage) -> UIImageView { 83 let imageView = UIImageView() 84 imageView.frame = frame 85 imageView.image = image 86 return imageView 87 } 88 89 //ピースイメージビューの生成 90 func makePieceImageView(frame: CGRect, index: Int, 91 picture: UIImage, piece: UIImage) -> UIImageView { 92 UIGraphicsBeginImageContextWithOptions(frame.size, false, 0) 93 picture.draw( 94 in: CGRect(x: CGFloat(-75*(index%4)), 95 y: CGFloat(-75*Int(index/4)), 96 width: 300, height: 300)) 97 piece.draw(in: CGRect(x: 0, y: 0, width: 75, height: 75)) 98 let image: UIImage = UIGraphicsGetImageFromCurrentImageContext()! 99 UIGraphicsEndImageContext() 100 return makeImageView(frame: frame, image: image) 101 } 102 103 //イメージボタンの生成 104 func makeButton(frame: CGRect, image: UIImage, tag: Int) -> UIButton { 105 let button = UIButton(type: UIButtonType.custom) 106 button.frame = frame 107 button.setImage(image, for: UIControlState.normal) 108 button.tag = tag 109 button.addTarget(self, action: #selector(onClick(sender:)), 110 for: UIControlEvents.touchUpInside) 111 return button 112 } 113 114 //==================== 115 //タッチイベント 116 //==================== 117 //ボタンクリック時に呼ばれる 118 @objc func onClick(sender: UIButton) { 119 if sender.tag == BTN_START { 120 //シャッフルの実行(8) 121 _shuffle = 20 122 while _shuffle > 0 { 123 if movePiece(tx: rand(num: 4), ty: rand(num: 4)) {_shuffle -= 1} 124 } 125 for i in 0..<16 { 126 let dx: CGFloat = 30+75*CGFloat(i%4) 127 let dy: CGFloat = 180+75*CGFloat(i/4) 128 _piece[_data[i]].frame = 129 CGRect(x: dx, y: dy, width: 75, height: 75) 130 } 131 132 //ゲーム開始 133 _titleLabel.text = "15 Puzzle" 134 _piece[15].alpha = 0 135 _startButton.alpha = 0 136 } 137 } 138 139 //タッチ開始時に呼ばれる 140 override func touchesBegan(_ touches: Set<UITouch>, with event: UIEvent?) { 141 if _startButton.alpha != 0 { 142 return 143 } 144 //タッチ位置からピースの列番号と行番号を求める(3) 145 let pos = touches.first!.location(in: _gameView) 146 if 30 < pos.x && pos.x < 330 && 180 < pos.y && pos.y < 480 { 147 let tx = Int((pos.x-30)/75) 148 let ty = Int((pos.y-180)/75) 149 movePiece(tx: tx, ty: ty) 150 } 151 } 152 153 //ピースの移動 154 func movePiece(tx: Int, ty: Int) -> Bool { 155 //空きマスの行番号と列番号を求める(4) 156 var fx = 0 157 var fy = 0 158 for i in 0..<16 { 159 if _data[i] == 15 { 160 fx = i%4 161 fy = Int(i/4) 162 break 163 } 164 } 165 if (fx == tx && fy == ty) || (fx != tx && fy != ty) { 166 return false 167 } 168 169 //ピースを上にスライド(5) 170 if fx == tx && fy < ty { 171 for i in fy..<ty { 172 _data[fx+i*4] = _data[fx+i*4+4] 173 } 174 _data[tx+ty*4] = 15 175 } 176 //ピースを下にスライド(5) 177 else if fx == tx && fy > ty { 178 for i in fy.stride(to: ty, by: -1) { 179       //エラー発生 180 _data[fx+i*4] = _data[fx+i*4-4] 181 } 182 _data[tx+ty*4] = 15 183 } 184 //ピースを左にスライド(5) 185 else if fy == ty && fx < tx { 186 for i in fx..<tx { 187 _data[i+fy*4] = _data[i+fy*4+1] 188 } 189 _data[tx+ty*4] = 15 190 } 191 //ピースを右にスライド(5) 192 else if fy == ty && fx > tx { 193 for i in fx.stride(to: tx, by: -1) { 194       //エラー発生 195 _data[i+fy*4]=_data[i+fy*4-1] 196 } 197 _data[tx+ty*4] = 15 198 } 199 200 //シャッフル時はピースの移動アニメとクリアチェックは行わない 201 if _shuffle > 0 { 202 return true 203 } 204 205 //ピースの移動アニメとクリアチェック(6) 206 var clearCheck = 0 207 for i in 0..<16 { 208 let dx: CGFloat = 30+75*CGFloat(i%4) 209 let dy: CGFloat = 180+75*CGFloat(i/4) 210 211 //ピースの移動アニメ 212 if _data[i] != 15 { 213 UIView.beginAnimations("anime0", context: nil) 214 UIView.setAnimationDuration(0.3) 215 _piece[_data[i]].frame = CGRect(x: dx, y: dy, width: 75, height: 75) 216 UIView.commitAnimations() 217 } else { 218 _piece[_data[i]].frame = CGRect(x: dx, y: dy, width: 75, height: 75) 219 } 220 221 //クリアチェック 222 if _data[i] == i {clearCheck += 1} 223 } 224 225 //ゲームのクリア判定(7) 226 if clearCheck == 16 { 227 _titleLabel.text = "Clear!" 228 _startButton.alpha = 100 229 230 //ピースの出現アニメ 231 UIView.beginAnimations("anime1", context: nil) 232 UIView.setAnimationDuration(0.6) 233 _piece[15].alpha = 100 234 UIView.commitAnimations() 235 } 236 return true 237 } 238 239 240 241 242 243 244 245 246 247 override func didReceiveMemoryWarning() { 248 super.didReceiveMemoryWarning() 249 // Dispose of any resources that can be recreated. 250 } 251 func rand(num: UInt32) -> Int { 252 return Int(arc4random()%num) 253 } 254 255 256}

fuzzballさん、これがエラーの出ている場所です

気になる質問をクリップする

クリップした質問は、後からいつでもMYページで確認できます。

またクリップした質問に回答があった際、通知やメールを受け取ることができます。

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

guest

回答1

0

ベストアンサー

A.stride(to: B, by: C)stride(from: A, to: B, by: C)

投稿2018/03/08 05:01

編集2018/03/08 05:04
fuzzball

総合スコア16731

バッドをするには、ログインかつ

こちらの条件を満たす必要があります。

naokun

2018/03/08 05:03

from: ここから、to: ここまで、by: 間隔ということは理解しているのですが......自分でも大分前に書いたものなのでstrideの周りがよくわからなくなってしまっているのです。この場合、どうしたらいいのでしょうか?
naokun

2018/03/08 05:10

一応直して見たのですがこの様なエラーが...... Cannot invoke 'stride' with an argument list of type '(from: Int, to: Int, by: Int)' 何故Intが使えないのでしょうか
fuzzball

2018/03/08 05:14

どう直したのか書いてもらわないと分かるわけ無いでしょう?
naokun

2018/03/08 05:15

すみません、書き忘れていましたね。 stride(from: tx, to: ty, by: -1) stride(from: ty, to: tx, by: -1) という風に直しました
fuzzball

2018/03/08 05:29

そのようなエラーは出ませんが。 let tx = 10 let ty = 5 for i in stride(from: tx, to: ty, by: -1) {print(i)} //=> 10 9 8 7 6 5 4
fuzzball

2018/03/08 05:38

strideの前に tx. とか ty. (私の回答で言うと A. )が付いたままではないですか?
naokun

2018/03/08 05:38

追記させていただきました。画像をご参照ください。
naokun

2018/03/08 05:39

付いたままというのはどういうことなんでしょうか
naokun

2018/03/08 05:40

すみません、解決しました!!ありがとうございます!!!
guest

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

15分調べてもわからないことは
teratailで質問しよう!

ただいまの回答率
85.50%

質問をまとめることで
思考を整理して素早く解決

テンプレート機能で
簡単に質問をまとめる

質問する

関連した質問