hello i am begginer to swift i get an error "Cannot assign value of type 'Character' to type '[String]'" how can i fix that my brain is now lost in this code blog enter code here
import UIKit import FirebaseFirestore import FirebaseAuth import FirebaseDatabase import FirebaseStorage
class PhoneViewController: UIViewController {
@IBOutlet weak var tableView: UITableView!
var phoneModelText = [String]()
var imeiAdressText = [String]()
var userNameText = [String]()
var idText = [String]()
var phoneNumberText = [String]()
var detailsText = [String]()
var dateText = [String]()
var priceText = [String]()
var adressText = [String]()
var selectedPhoneModelText = ""
var selectedimeiAdressText = ""
var selecteduserNameText = ""
var selectedidText = ""
var selectedphoneNumberText = ""
var selecteddetailsText = ""
var selecteddateText = ""
var selectedpriceText = ""
var selectedadressText = ""
override func viewDidLoad() {
super.viewDidLoad()
navigationController?.navigationBar.prefersLargeTitles = true
tableView.dataSource = self
tableView.delegate = self
getdata()
}
func makeAlert(titleInput: String, messageInput : String) {
let alert = UIAlertController(title: titleInput, message: messageInput, preferredStyle: UIAlertController.Style.alert)
let okButton = UIAlertAction(title: "Ok", style: UIAlertAction.Style.default, handler: nil)
alert.addAction(okButton)
present(alert, animated: true, completion: nil)
}
func fetchBook(documentId: String) {
let db = Firestore.firestore()
let docRef = db.collection("Databases").document(documentId)
docRef.getDocument { document, error in
if let error = error as NSError? {
self.makeAlert(titleInput: "alert", messageInput: "\(error.localizedDescription)")
}
else {
if let document = document {
let id = document.documentID
let data = document.data()
let phonemodel = data?["phoneName"] as? String ?? ""
let imeiadress = data?["imeiNumberText"] as? Int ?? 0
let username = data?["userNameText"] as? String ?? ""
let idcard = data?["idCardtext"] as? Int ?? 0
let phonenumber = data?["phoneNumberText"] as? Int ?? 0
let adress = data?["adressNameText"] as? String ?? ""
let details = data?["detailSectionText"] as? String ?? ""
let date = data?["currentDateText"] as? String ?? ""
let price = data?["priceValueText"] as? Int ?? 0
let image = data?["imageurl"] as? String ?? ""
DispatchQueue.main.async {
self.selectedphoneNumberText = phonemodel
self.phoneModelText.text = phonemodel
self.imeiAdressText.text = String(imeiadress)
self.userNameText.text = username
self.idText.text = String(idcard)
self.phoneNumberText.text = String(phonenumber)
self.adressText.text = adress
self.detailsText.text = details
self.dateText.text = date
self.priceText.text = String(price)
}
}
}
}
}
}
extension PhoneViewController : UITableViewDataSource,UITableViewDelegate {
func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return phoneModelText.count
}
func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = UITableViewCell()
cell.textLabel?.text = phoneModelText[indexPath.row]
return cell
}
func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) {
// how i customizing there
performSegue(withIdentifier: "toPhoneListView", sender: nil)
}
override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
if segue.identifier == "toPhoneListView" {
let destinationVC = segue.destination as! PhoneListViewController
destinationVC.selectedPhoneModelText
destinationVC.selectedimeiAdressText
destinationVC.selecteduserNameText
destinationVC.selectedidText
destinationVC.selectedphoneNumberText
destinationVC.selecteddetailsText
destinationVC.selecteddateText
destinationVC.selectedpriceText
destinationVC.selectedadressText
}
}
}
CodePudding user response:
This is how your phoneModelText is defined
var phoneModelText = [String]()
that indicates that phoneModelText is an array of strings, so it would look something like this
phoneModelText[0] = "Some String"
phoneModelText[1] = "Another string"
but then later you're attempting to assign string to that array
self.phoneModelText.text = phonemodel
And that's not how arrays work. If you want to add phoneModel
to the array it would be this
self.phoneModelText.append(phoneModel) //assume phoneModel = "yet another string"
so then the array would look like this
phoneModelText[0] = "Some String"
phoneModelText[1] = "Another string"
phoneModelText[2] = "yet another string"
In general I would suggest naming your vars so they more represent what they contain - instead of phoneModelText, call it phoneModelTextArray. That wil reduce confusion and make the code more readable.
As far as a solution, it's not clear why there are a bunch of arrays
var phoneModelText = [String]()
var imeiAdressText = [String]()
var userNameText = [String]()
but I suggest changing all of that around. One option is to define a class with properties and then have an array of classes
class ContactClass {
var id = ""
var phoneText = ""
var imeiAdressText = ""
var userNameText = ""
}
and then an array of classes within your controller
var contactArray = [ContactClass]()
and then lastly, when reading data from Firebase, instantiate the class, populate the class properties and add the class to the array
else {
if let document = document {
let contact = ContactClass()
contact.id = document.documentID
contact.phoneText = data?["phoneName"] as? String ?? ""
contact.imeiAdressText = data?["imeiNumberText"] as? Int ?? 0
contact.userNameText = data?["userName"] as? String ?? ""
self.contactArray.append(contact)