0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?

More than 5 years have passed since last update.

【質問】ビルド時に[The process has been returned to the state before expression evaluation.]のエラーが出る

Posted at
  • Swift3.0を使用しています
  • プログラミング初心者です

Firebase内のストレージ画像を呼び出し、KingfisherでUIImageに変換する

以下コードです。若干冗長な感じになってますがとりあえずコードが通ったら集約する予定です。

  • 画像を呼び出す部分
ChatViewController.swift
class ChatViewController: JSQMessagesViewController {
    
    var sendText:String = ""  //チャット相手のuidが入ってくる
    var recieverId:String!
    var userId : String!
    var nickName : String!
    var roomKey : String!
    var messages = [JSQMessage]()
    var senderProfileImage : UIImageView!
    var senderProfileImageInChat : JSQMessagesAvatarImage!
    var senderProfileImageUrl : String!
    var recieverProfileImage : UIImageView!
    var recieverProfileImageInChat : JSQMessagesAvatarImage!
    var recieverProfileImageUrl : String!
    var item = [NSDictionary]()
    
    //自分のプロファイルイメージのURLを呼び出す
    func getSenderProfileImage(uid :String!, completion:@escaping (_ imageUrl: String) -> Void){
        
        let urlData: String = "profile/" + uid + ".jpg"
        let storage = Storage.storage() 
        let storageRef = storage.reference(forURL: " *firebaseUrl* ")
        
        storageRef.child(urlData).downloadURL { (URL, error) -> Void in 
            guard let urlString = URL?.absoluteString else {  
                return 
            }
            // self.recieverProfileImageUrl = urlString
            completion(urlString)
        }
    }

  • viewDidLoadの部分
ChatViewController.swift
    override func viewDidLoad() {
        super.viewDidLoad()
        self.topContentAdditionalInset = 30
        
        recieverId = sendText
        let ref = Database.database().reference()
    
        
        userId = Auth.auth().currentUser?.uid
        self.senderDisplayName = ""
        senderId = userId
        
        //nicknamesを取得
        ref.child("profile").child(userId!).observeSingleEvent(of: .value, with: { (snapshot) in
            
            let value = snapshot.value as? NSDictionary
            self.senderDisplayName = value?["nickname"] as? String ?? ""
            self.nickName = self.senderDisplayName
            
        }) { (error) in
            print(error.localizedDescription)
        }
        
        
        //タイトルをセット(本当は相手の名前にしたい)
        self.title = "メッセージ"

        //自分のプロファイルイメージをセット
        getSenderProfileImage(uid: senderId) { (imageUrl) in
            let i = (imageUrl)
            //var url: URL = URL(string: i)!
            if let url :URL = URL(string: i) {
            self.recieverProfileImage!.kf.setImage(with: url) 
            }
            let profileImage = self.recieverProfileImage.image  
           self.recieverProfileImageInChat = JSQMessagesAvatarImageFactory.avatarImage(with: profileImage, diameter: 64)
            self.collectionView.reloadData()
        }

これでコンパイルエラーはなさそうなのですが、
この self.recieverProfileImage!.kf.setImage(with: url) でビルド時にクラッシュします。

この部分で po self.recieverProfileImage!.kf.setImage(with: url) をすると以下のエラーが返却されます。

fatal error: unexpectedly found nil while unwrapping an Optional value
error: Execution was interrupted, reason: EXC_BAD_INSTRUCTION (code=EXC_I386_INVOP, subcode=0x0).
The process has been returned to the state before expression evaluation.

この原因がわからず、困っています。
アドバイスいただけるとありがたいです。解決した場合にこちらで記載します。

0
0
3

Register as a new user and use Qiita more conveniently

  1. You get articles that match your needs
  2. You can efficiently read back useful information
  3. You can use dark theme
What you can do with signing up
0
0

Delete article

Deleted articles cannot be recovered.

Draft of this article would be also deleted.

Are you sure you want to delete this article?