import
1 2class SettingsViewController: UIViewController, UIImagePickerControllerDelegate, UINavigationControllerDelegate, UITextFieldDelegate { 3 4 5 @IBOutlet weak var usericonView: UIImageView! 6 7 @IBOutlet weak var nickNameTextField: UITextField! 8 9 @IBOutlet weak var userNameTextField: UITextField! 10 11 @IBOutlet weak var sendEmailTextField: UITextField! 12 13 @IBOutlet weak var sendPhoneNumber: UITextField! 14 15 @IBOutlet weak var nextSendEmailTextField: UITextField! 16 17 @IBOutlet weak var nextSendPhoneNumber: UITextField! 18 19 20 21 @IBOutlet weak var nickNameLabel: UILabel! 22 23 @IBOutlet weak var userNameLabel: UILabel! 24 25 @IBOutlet weak var sendEmailLabel: UILabel! 26 27 @IBOutlet weak var sendPhoneLabel: UILabel! 28 29 @IBOutlet weak var nextSendEmailLabel: UILabel! 30 31 @IBOutlet weak var nextSendPhoneLabel: UILabel! 32 33 34 var nickNameText:String = "ニックネームを入力" 35 var userNameText:String = "名前を入力" 36 var sendEmailText:String = "送り先Emailを入力" 37 var sendPhone:String = "電話番号を入力" 38 var nextSendEmailText:String = "送り先Email-2を入力" 39 var nextSendPhone:String = "電話番号-2を入力" 40 41 42 let userDefaults = UserDefaults.standard 43 44 45 46 override func viewDidLoad() { 47 super.viewDidLoad() 48 49 50 nickNameTextField.delegate = self 51 userNameTextField.delegate = self 52 sendEmailTextField.delegate = self 53 sendPhoneNumber.delegate = self 54 nextSendEmailTextField.delegate = self 55 nextSendPhoneNumber.delegate = self 56 57 userDefaults.register(defaults: ["NickName": "ニックネームを入力"]) 58 nickNameLabel.text = readData() 59 60 userDefaults.register(defaults: ["UserName": "名前を入力"]) 61 userNameLabel.text = readData() 62 63 userDefaults.register(defaults: ["SendEmail": "Emailを入力"]) 64 sendEmailLabel.text = readData() 65 66 userDefaults.register(defaults: ["SendPhone": "電話番号を入力"]) 67 sendPhoneLabel.text = readData() 68 69 userDefaults.register(defaults: ["NextSendEmail": "Emailを入力"]) 70 nextSendEmailLabel.text = readData() 71 72 userDefaults.register(defaults: ["NextSendPhone": "電話番号を入力"]) 73 nextSendPhoneLabel.text = readData() 74 75 } 76 77 //アルバムの写真を選択してアイコンとして表示する。 78 @IBAction func toAlbumButton(_ sender: Any) { 79 80 let picker = UIImagePickerController() 81 picker.sourceType = .photoLibrary 82 picker.delegate = self 83 present(picker, animated: true) 84 self.present(picker, animated: true, completion: nil) 85 86 } 87 88 func imagePickerController(_ picker: UIImagePickerController, didFinishPickingMediaWithInfo info: [UIImagePickerController.InfoKey : Any]) { 89 if let selectedImage = info[.originalImage] as? UIImage{ 90 usericonView.image = selectedImage 91 } 92 self.dismiss(animated: true) 93 } 94 95 func imagePickerControllerDidCancel(_ picker: UIImagePickerController) { 96 self.dismiss(animated: true, completion: nil) 97 } 98 99 //テキスト入力設定 100 101 func textFieldShouldReturn(_ textField: UITextField) -> Bool{ 102 nickNameText = textField.text! 103 nickNameLabel.text = nickNameText 104 105 userNameText = textField.text! 106 userNameLabel.text = userNameText 107 108 sendEmailText = textField.text! 109 sendEmailLabel.text = sendEmailText 110 111 sendPhone = textField.text! 112 sendPhoneLabel.text = sendPhone 113 114 nextSendEmailText = textField.text! 115 nextSendEmailLabel.text = nextSendEmailText 116 117 nextSendPhone = textField.text! 118 nextSendPhoneLabel.text = nextSendPhone 119 120 121 textField.resignFirstResponder() 122 123 saveData(str: nickNameText) 124 saveData(str: userNameText) 125 saveData(str: sendEmailText) 126 saveData(str: sendPhone) 127 saveData(str: nextSendEmailText) 128 saveData(str: nextSendPhone) 129 130 return true 131 132 } 133 134 func saveData(str: String){ 135 userDefaults.set(str, forKey: "NickName") 136 userDefaults.set(str, forKey: "UserName") 137 userDefaults.set(str, forKey: "SendEmail") 138 userDefaults.set(str, forKey: "SendPhone") 139 userDefaults.set(str, forKey: "NextSendEmail") 140 userDefaults.set(str, forKey: "NextSendPhone") 141 } 142 143 func readData() -> String { 144 let str: String = userDefaults.object(forKey: "NickName") as! String 145 146 let _: String = userDefaults.object(forKey: "UserName") as! String 147 148 let _: String = userDefaults.object(forKey: "SendEmail") as! String 149 150 let _: String = userDefaults.object(forKey: "SendPhone") as! String 151 152 let _: String = userDefaults.object(forKey: "NextSendEmail") as! String 153 154 let _: String = userDefaults.object(forKey: "NextSendPhone") as! String 155 156 return str 157 158 } 159 160 161 @IBAction func hozonButtton(_ sender : AnyObject) { 162 163 // Key の値を削除 164 userDefaults.removeObject(forKey: "NickName") 165 userDefaults.removeObject(forKey: "UserName") 166 userDefaults.removeObject(forKey: "SendEmail") 167 userDefaults.removeObject(forKey: "SendPhone") 168 userDefaults.removeObject(forKey: "NextsendEmail") 169 userDefaults.removeObject(forKey: "NextSendPhone") 170 171 172 173 userDefaults.set(true, forKey: "NickName") 174 let str: String = userDefaults.object(forKey: "NickName") as! String 175 nickNameLabel.text = str 176 177 userDefaults.set(true, forKey: "UserName") 178 let _: String = userDefaults.object(forKey: "UserName") as! String 179 nickNameLabel.text = str 180 181 userDefaults.set(true, forKey: "SendEmail") 182 let _: String = userDefaults.object(forKey: "SendEmail") as! String 183 nickNameLabel.text = str 184 185 userDefaults.set(true, forKey: "SendPhone") 186 let _: String = userDefaults.object(forKey: "SendPhone") as! String 187 nickNameLabel.text = str 188 189 userDefaults.set(true, forKey: "NextSendemail") 190 let _: String = userDefaults.object(forKey: "NextSendEmail") as! String 191 nickNameLabel.text = str 192 193 userDefaults.set(true, forKey: "NextSendPhone") 194 let _: String = userDefaults.object(forKey: "NextSendPhone") as! String 195 nickNameLabel.text = str 196 197 nickNameLabel.text = str 198 userNameLabel.text = str 199 sendEmailLabel.text = str 200 sendPhoneLabel.text = str 201 nextSendEmailLabel.text = str 202 nextSendPhoneLabel.text = str 203 204 205 print() 206 207 } 208 209 210} 211 212コード
全くの初心者でこのエラーがどこから来てるのか分かりません。 ご指導いただければ存じます。 一通りコードを書いてビルドして動作を確認してましたが、155行目で 表題のエラーメッセージが発生しました。 ### Thread 1: Fatal error: Unexpectedly found nil while unwrapping an Optional value
該当コードは let _: String = userDefaults.object(forKey: "UserName") as! String です。
どこにnilになるのか分かりません。
### 該当のソースコード ### 試したこと いくつかの関数を見直しました。 ### 補足情報(FW/ツールのバージョンなど) Xcodwは最新です。
回答4件
あなたの回答
tips
プレビュー