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

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

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

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

Q&A

解決済

1回答

764閲覧

xcodeエラー 「unexpectedly found nil while unwrapping an Optional value」

sugi-

総合スコア0

Swift

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

0グッド

0クリップ

投稿2021/07/15 05:15

編集2021/07/15 05:18

このコード( loadDBModel.loadUsersProfile(gender: userData["gender"] as! String))のところでThread 1: Fatal error: Unexpectedly found nil while implicitly unwrapping an Optional value
のエラーが出てしまいます。。

オプショナル型をアンラップしろっていう意味ですか??

そしてどこを アンラップすれば良いかわからないです。。。。

よろしくお願いします!

コード class HumansViewController: UIViewController,GetProfileDataProtocol,UICollectionViewDelegate, UICollectionViewDataSource,UICollectionViewDelegateFlowLayout{ @IBOutlet weak var ashiatoButton: UIButton! @IBOutlet weak var searchButton: UIButton! @IBOutlet weak var collectionView: UICollectionView! var searchORNot = Bool() let sectionInsets = UIEdgeInsets(top: 10.0, left: 2.0, bottom: 2.0, right: 2.0) let itemsPerRow:CGFloat = 2 var userDataModelArray = [UserDataModel]() override func viewDidLoad() { super.viewDidLoad() // Do any additional setup after loading the view. } override func viewWillAppear(_ animated: Bool) { super.viewWillAppear(animated) self.navigationController?.isNavigationBarHidden = true //もしauthitenuserに登録されていたら if Auth.auth().currentUser?.uid != nil && searchORNot == false{ collectionView.delegate = self collectionView.dataSource = self //自分のユーザーデータを取り出す let userData = KeyChainConfig.getKeyArrayData(key: "userData") //受信します let loadDBModel = LoadDBModel() loadDBModel.getProfileDataProtocol = self //ここでエラーが発生します loadDBModel.loadUsersProfile(gender: userData["gender"] as! String) }else if Auth.auth().currentUser?.uid != nil && searchORNot == true{ //検索から返ってきたのでロードする必要が無い collectionView.reloadData() //この文はユーザー登録されていない状態なのでユーザー登録の画面を出す }else{ performSegue(withIdentifier: "inputVC", sender: nil) } } func getProfileData(userDataModelArray: [UserDataModel]) { self.userDataModelArray = userDataModelArray collectionView.reloadData() } func numberOfSections(in collectionView: UICollectionView) -> Int { return 1 } func collectionView(_ collectionView: UICollectionView, numberOfItemsInSection section: Int) -> Int { return userDataModelArray.count } //スクリーンのサイズに応じたセルのサイズを変える func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, sizeForItemAt indexPath: IndexPath) -> CGSize { let paddingSpace = sectionInsets.left * (itemsPerRow + 1) let availableWidth = view.frame.width - paddingSpace let widthPerItem = availableWidth / itemsPerRow return CGSize(width: widthPerItem, height: widthPerItem + 42) } func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, insetForSectionAt section: Int) -> UIEdgeInsets { return sectionInsets } //セルの行間 func collectionView(_ collectionView: UICollectionView, layout collectionViewLayout: UICollectionViewLayout, minimumLineSpacingForSectionAt section: Int) -> CGFloat { return 10 } func collectionView(_ collectionView: UICollectionView, cellForItemAt indexPath: IndexPath) -> UICollectionViewCell { //セルと構築、返す let cell = collectionView.dequeueReusableCell(withReuseIdentifier: "Cell", for: indexPath) //セルに効果 影 cell.layer.cornerRadius = cell.frame.width/2 cell.layer.borderWidth = 0.0 cell.layer.shadowColor = UIColor.black.cgColor cell.layer.shadowOffset = CGSize(width: 0, height: 0) cell.layer.shadowRadius = 5.0 cell.layer.shadowOpacity = 1.0 cell.layer.masksToBounds = true let imageView = cell.contentView.viewWithTag(1) as! UIImageView imageView.sd_setImage(with: URL(string: userDataModelArray[indexPath.row].profileImageString!), completed: nil) imageView.layer.cornerRadius = imageView.frame.width/2 let ageLabel = cell.contentView.viewWithTag(2) as! UILabel ageLabel.text = userDataModelArray[indexPath.row].age let prefectureLabel = cell.contentView.viewWithTag(3) as! UILabel prefectureLabel.text = userDataModelArray[indexPath.row].prefecture let onLineMarkImageView = cell.contentView.viewWithTag(4) as! UIImageView onLineMarkImageView.layer.cornerRadius = onLineMarkImageView.frame.width/2 if userDataModelArray[indexPath.row].onlineORNot == true { onLineMarkImageView.image = UIImage(named: "online") }else{ onLineMarkImageView.image = UIImage(named: "offline") } return cell } func collectionView(_ collectionView: UICollectionView, didSelectItemAt indexPath: IndexPath) { let profileVC = self.storyboard?.instantiateViewController(identifier: "profileVC") as! ProfileViewController profileVC.userDataModel = userDataModelArray[indexPath.row] self.navigationController?.pushViewController(profileVC, animated: true) } }
  • 環境xcodeVersion 12.5.1 (12E507)

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

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

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

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

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

guest

回答1

0

自己解決

ログインの場所を消していました。

投稿2021/07/15 05:32

sugi-

総合スコア0

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

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

あなたの回答

tips

太字

斜体

打ち消し線

見出し

引用テキストの挿入

コードの挿入

リンクの挿入

リストの挿入

番号リストの挿入

表の挿入

水平線の挿入

プレビュー

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

ただいまの回答率
85.50%

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

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

質問する

関連した質問