将[String]转换为String

时间:2017-04-28 14:27:06

标签: swift firebase firebase-realtime-database

我试图通过didSelectRowAt将我的TableViewCell中的用户名传递给另一个表单。 TableViewCell中的用户名是通过Array

生成的
import UIKit
import Firebase
import FirebaseDatabase

var userNames = [String] ()
var planType = [String] ()
var planDesc = [String] ()
var planDate = [String] ()

//Index contains item in array that we want to display
var myIndex = 0;

class RequestsTableViewController: UITableViewController
{

@IBOutlet weak var lblUsernameX: UILabel!

var userNameX = ""

var dbReference: FIRDatabaseReference!
var usersReference : FIRDatabaseReference!

override func viewDidLoad()
{
    super.viewDidLoad()

        lblUsernameX.text = userNameX

    //reference to database
    self.dbReference = FIRDatabase.database().reference()

    //reference to users in database
    self.usersReference = dbReference.child("MH_PlanRequest")//.queryOrdered(byChild: "username")


    //data comes into a snapshot object
    self.usersReference.observe(.childAdded, with: { (snapshot) in

        //var usersSnapshotArray = [FIRDataSnapshot]()

        //from the snapshot get the entry as key-value (KV)pair
        //use a swift native Dictionary object to hold the KV pair
        let snapshotValue = snapshot.value as! Dictionary<String, String>

        //use the keys to get the values
        let userName = snapshotValue["username"]! as String
        let date = snapshotValue["date"]! as String
        let type = snapshotValue["plantype"]! as String
        let desc = snapshotValue["plandesc"]! as String
        //print(userName)
        //print(date)
        //print(planType)

        userNames.append(userName)
        planDate.append(date)
        planType.append(type)
        planDesc.append(desc)

        self.tableView.reloadData()
    })

    //print("Reloading Data....")
    //tableView.reloadData()
    //print("Data Reloaded....")
    //tableView.reloadData()


}

该阵列来自我的Firebase数据存储

当我点击单元格并且它转到另一种形式时,我得到了这个错误。

错误:

Cannot convert value of type '[String]' to expected argument type 'String'

另一个表单包含标签,并根据用户名标签加载ViewDidLoad,数据来自Firebase中的用户。

import UIKit
import Firebase
import FirebaseDatabase

class ManageRequestViewController: UIViewController
{

@IBOutlet weak var lblUsernameX: UILabel!
var userNameX = ""

@IBOutlet weak var lblDate: UILabel!
@IBOutlet weak var lblUsername: UILabel!
@IBOutlet weak var lblPlanType: UILabel!
@IBOutlet weak var lblActivityLevel: UILabel!
@IBOutlet weak var lblWeight: UILabel!
@IBOutlet weak var lblAge: UILabel!
@IBOutlet weak var lblHeight: UILabel!
@IBOutlet weak var lblCalorie: UILabel!
@IBOutlet weak var txtDesc: UITextView!
@IBOutlet weak var txtDietPlan: UITextView!

var dbActivityLevel = ""
var dbWeight = ""
var dbAge = ""
var dbHeight = ""



var dbReference: FIRDatabaseReference!
var usersReference : FIRDatabaseReference!

override func viewDidLoad() {
    super.viewDidLoad()

    lblUsernameX.text = userNameX

    lblUsername.text = userNames[myIndex]
    //lblDesc.text = petDesc[myIndex]


    /*Code to dismiss keyboard on background tap
    let tap: UITapGestureRecognizer = UITapGestureRecognizer(target: self, action: #selector(LoginViewController.dismissKeyboard))

    view.addGestureRecognizer(tap)
     */

    //reference to database
    self.dbReference = FIRDatabase.database().reference()


    //reference to username in database
    self.usersReference = dbReference.child("MH_Accounts").child(userNames[myIndex])//.queryOrdered(byChild: "username")

    //data comes into a snapshot object
    //self.usersReference.observeSingleEvent(of: .value, with: { (snapshot) in
        //data comes into a snapshot object
        self.usersReference.observe(.childAdded, with: { (snapshot) in

        let snapshotValue = snapshot.value as! [String: AnyObject]

        //use the key "username" to get the details of username
        self.dbWeight = snapshotValue["weight"]! as! String
        self.dbAge = snapshotValue["age"]! as! String
        self.dbHeight = snapshotValue["height"]! as! String
        self.dbActivityLevel = snapshotValue["activitylevel"]! as! String

        self.lblUsername.text = userNames[myIndex]
        self.lblActivityLevel.text = self.dbActivityLevel
        self.lblWeight.text = self.dbWeight
        self.lblAge.text = self.dbAge
        self.lblHeight.text = self.dbHeight

    })

firebase的JSON:

    {
  "MH_Accounts" : {
    "admin" : {
      "activitylevel" : "Light",
      "age" : "44",
      "email" : "f@f.com",
      "gender" : "Male",
      "height" : "170",
      "password" : "password",
      "weight" : "45"
    },
    "test123" : {
      "activitylevel" : "Light",
      "age" : "21",
      "email" : "iO@hotmail.com",
      "gender" : "Male",
      "height" : "171",
      "password" : "password",
      "weight" : "83"
    }
  },
  "MH_PlanRequest" : {
    "test111" : {
      "date" : "03-25-2016",
      "plandesc" : "haha test2",
      "plantype" : "Bulking",
      "username" : "test111"
    },
    "test333" : {
      "date" : "04-26-2017",
      "plandesc" : "haha test",
      "plantype" : "Weight Loss",
      "username" : "test333"
    }
  },
  "MH_Progress" : {
    "admin" : {
      "04-23-2017" : "https://firebasestorage.googleapis.com/v0/b/myhealth-5ee8b.appspot.com/o/images%2Fadmin04-23-2017.jpg?alt=media&token=c402f576-4e29-4809-9cb3-3c8849708129"
    }
  }
}

1 个答案:

答案 0 :(得分:0)

括号表示该类型是一个字符串数组。看起来self.usersReference是一个字符串,.child(usernames)返回一个数组。您必须弄清楚是否要从数组中获取元素,或者您是否首先调用正确的方法。

我不知道Firebase所以无法帮助那里。

相关问题