前提・実現したいこと
https://www.yukisako.xyz/entry/swift-kusoapps-summery
このページに載っている2つ目のアプリを作ろうと思っています。
■実装中にif文のところでエラーメッセージが発生しました。
ViewController.swiftのコードです。
発生している問題・エラーメッセージ
Value of optional type 'Int?' must be unwrapped to a value of type 'Int'
該当のソースコード
swift
1 2import UIKit 3 4class ViewController: UIViewController { 5 @IBOutlet var input: UITextField! 6 @IBOutlet var output: UILabel! 7 8 var random = Int(arc4random_uniform(101)) 9 10 var count = 0 11 12 13 @IBAction func submit(sender: AnyObject) { 14 15 16 let answer = Int(input.text!) 17 count += 1 18 19 if answer < 0 || answer > 100 { 20 output.text = "入力は0〜100にしてください" 21 } else if answer < random { 22 output.text = "もっと高かった♪♪" 23 } else if answer > random { 24 output.text = "もっと低かった...!!" 25 } else { 26 output.text = "正解!!! (count) 回目で当たった!" 27 } 28 29 } 30 31 override func viewDidLoad() { 32 super.viewDidLoad() 33 34 print(random) 35 // Do any additional setup after loading the view, typically from a nib. 36 } 37 38 override func didReceiveMemoryWarning() { 39 super.didReceiveMemoryWarning() 40 // Dispose of any resources that can be recreated. 41 } 42 43 44} 45 46
試したこと
()をつけてみた
補足情報(FW/ツールのバージョンなど)
Xcode Version 10.1 (10B61)
回答1件
あなたの回答
tips
プレビュー
バッドをするには、ログインかつ
こちらの条件を満たす必要があります。
2018/12/05 01:40