Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
234 views
in Technique[技术] by (71.8m points)

arrays - func not executed inside if statement swift

im trying to fetch data from firestore for certain user and what i mean here is if there is a user his name is Mark and he logged in to his account i want to fetch MarkCollection from the Firestore so i decided to use if statement but it doesn't work . here in the first example i can fetch the data but i want this data to be displayed for the user Mark only and in the second Example i used if statement but the problem is loadData1() cannot execute inside if statement.

First Example:

import UIKit
import FirebaseFirestore
import Firebase
import FirebaseAuth

class orderTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate {



@IBOutlet var userName: UITextField!

var db: Firestore!
var phone = [String]()
var reciept = [String]()
var price = [String]()
var amount = [String]()
var Area = [String]()

override func viewDidLoad() {
    super.viewDidLoad()
    
    
    loadData1()
}


func loadData1() {
    
    db.collection("markCollection").getDocuments() {
        
        (querySnapshot, err) in
        
        if let err = err
        
        {
            print("Error getting documents: (err)");
        }
        else
        {
            for document in querySnapshot!.documents {
                
                self.Area.append(document.get("area") as? String ?? "")
                self.phone.append(document.get("phone") as? String ?? "")
                self.reciept.append(document.get("reciept") as? String ?? "")
                self.price.append(document.get("total price") as? String ?? "")
                self.amount.append(document.get("amount") as? String ?? "")
            }
        }
        self.order.reloadData()
    }
}
}

and here is the problem when i use the if statement , the loadData1() func cannot execute here is the code for Example 2:

import UIKit
import FirebaseFirestore
import Firebase
import FirebaseAuth

    class orderTableViewController: UIViewController, UITableViewDelegate, UITableViewDataSource, UITextFieldDelegate {
    
    
    
    @IBOutlet var userName: UITextField!
    
    var db: Firestore!
    var phone = [String]()
    var reciept = [String]()
    var price = [String]()
    var amount = [String]()
    var Area = [String]()
    
    override func viewDidLoad() {
        super.viewDidLoad()
        
        if userName.text == "Mark" {
         loadData1()
   
        }
  
        
    }
    
    
    func loadData1() {
        
        db.collection("markCollection").getDocuments() {
            
            (querySnapshot, err) in
            
            if let err = err
            
            {
                print("Error getting documents: (err)");
            }
            else
            {
                for document in querySnapshot!.documents {
                    
                    self.Area.append(document.get("area") as? String ?? "")
                    self.phone.append(document.get("phone") as? String ?? "")
                    self.reciept.append(document.get("reciept") as? String ?? "")
                    self.price.append(document.get("total price") as? String ?? "")
                    self.amount.append(document.get("amount") as? String ?? "")
                }
            }
            self.order.reloadData()
        }
    }
}

[1] [1]: https://i.stack.imgur.com/5YONj.png

question from:https://stackoverflow.com/questions/65878900/func-not-executed-inside-if-statement-swift

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

if you do not set a text "Mark" as a default text to the textField, it is nil at the moment that view cycle is in "ViewDidLoad". When a user enters something by using the keyboard, it happens in a different cycle.

You should check if userName.text == "Mark" when the user taps "done" button in the keyboard, or by checking in TextFields delegates while the user is typing.

to check the user input;

you can add this in viewDidLoad;

userName.addTarget(self, action: #selector(textFieldDidChange(_:)),
                              for: .editingChanged)

and then, in view controller;

@objc private func textFieldDidChange(_ textField: UITextField) {
    print(textField.text) // so that you can see whether you can successfully connect the outlet. if nothing prints, it means that you could not connect it properly.
    if textField.text = "Mark" {
     loadData1()
  }
}

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...