swiftでアプリを製作中なのですが、下のプログラミングのなかで何故そこにオプショナルがあるのか?と感じたため投稿させていただきました。
swift
1import UIKit 2 3class AddViewController: UIViewController,UITextFieldDelegate { 4 5 var array = [String]() 6 7 @IBOutlet var textField: UITextField! 8 9 10 override func viewDidLoad() { 11 super.viewDidLoad() 12 13 textField.delegate = self 14 // Do any additional setup after loading the view. 15 } 16 17 @IBAction func add(_ sender: Any) { 18 19 if UserDefaults.standard.object(forKey: "todo") != nil{ 20 array = UserDefaults.standard.object(forKey: "todo") as! [String] 21 } 22 23 array.append(textField.text!) 24 UserDefaults.standard.set(array, forKey: "todo") 25 26 self.navigationController?.popViewController(animated: true) 27 } 28 29 func textFieldShouldReturn(_ textField: UITextField) -> Bool { 30 31 textField.resignFirstResponder() 32 33 return true 34 } 35 /* 36 // MARK: - Navigation 37 38 // In a storyboard-based application, you will often want to do a little preparation before navigation 39 override func prepare(for segue: UIStoryboardSegue, sender: Any?) { 40 // Get the new view controller using segue.destination. 41 // Pass the selected object to the new view controller. 42 } 43 */ 44 45}
上記のプログラミングのなかの
swift
1array.append(textField.text!) 2```のtextに何故 オプショナルの!が付いているのかわかりません。 3もしわかるかたがいらっしゃいましたら、ぜひ教えていただきたいです。 4よろしくお願いいたします。
回答1件
あなたの回答
tips
プレビュー