Search code examples
iosswiftxcodeuiimageview

When i press button if image spesific image i wanna alert user, how can i do that? and i dont wanna save data to Firebase if image have specific name


If the image named "x" in my assets is defined to my imageView, I want to alert the screen when I click the upload button, but the definition I wrote with if does not work if there is a photo named "x" in the imageView. It still saves data to Firebase Database. Help me please.

    @IBAction func uploadButtonClicked(_ sender: Any) {
    if  self.imageView.image == UIImage(named: "x") && self.commentText.text! == "" {
        self.makeAlert(tittleInput: "Error", messageInput: "Please choose photo!")
    }else {
        
        let storage = Storage.storage()
        let storageReference = storage.reference()
        
        let mediaFolder = storageReference.child("media")
        
        if let data = imageView.image?.jpegData(compressionQuality: 0.5) {
            
            let uuid = UUID().uuidString
            
            let imageReference = mediaFolder.child("\(uuid).jpg")
            imageReference.putData(data, metadata: nil) { storageMetaData, error in
                if error != nil {
                    self.makeAlert(tittleInput: "Error", messageInput: error?.localizedDescription ?? "Error")
                } else {
                    imageReference.downloadURL { url, error in
                        if error == nil {
                            let imageURL = url?.absoluteString
                        
                            let fireStoreDatabase = Firestore.firestore()
                            
                            var fireStoreReference : DocumentReference? = nil
                            
                            let fireStorePost = ["imageUrl" : imageURL!, "postedBy": Auth.auth().currentUser?.email! , "postComment": self.commentText.text!,"date": FieldValue.serverTimestamp(), "like": 0] as [String: Any]
                           
                            fireStoreReference = fireStoreDatabase.collection("Posts").addDocument(data: fireStorePost, completion: { error in
                                if error != nil {
                                    self.makeAlert(tittleInput: "Error", messageInput: error?.localizedDescription ?? "Error")
                                   
                                } else {
                                    
                                    self.imageView.image = UIImage(named: "x")
                                    self.commentText.text = ""
                                    self.tabBarController?.selectedIndex = 0
                                }
                                
                            })
                      
                        }
                    }
                }
            }
        }     
    }
    

Solution

  • Your if statement looks incorrect:

    if self.imageView.image == UIImage(named: "x") && self.commentText.text! == "" {
    

    That says:

    • if
      • the imageView's image IS EQUAL TO UIImage(named: "x")
        • AND
      • the commentText field (or label?) IS EQUAL TO "" (an empty string)

    I think you want this:

    if self.imageView.image == UIImage(named: "x") || self.commentText.text! == "" {
    

    Which says:

    • if
      • the imageView's image IS EQUAL TO UIImage(named: "x")
        • OR
      • the commentText field (or label?) IS EQUAL TO "" (an empty string)