Swift 错误的可能原因是什么:类型“的值”;x";没有下标吗?
这是我的密码。我已经检查过了,但似乎无法纠正错误。我需要做的是从数据库中获取firebase信息并将其显示在屏幕上Swift 错误的可能原因是什么:类型“的值”;x";没有下标吗?,swift,Swift,这是我的密码。我已经检查过了,但似乎无法纠正错误。我需要做的是从数据库中获取firebase信息并将其显示在屏幕上 class homepage:UITableViewController, CLLocationManagerDelegate{ var people = [Userx]() public override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int
class homepage:UITableViewController, CLLocationManagerDelegate{
var people = [Userx]()
public override func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int {
return people.count
}
public override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
as! ViewControllerTableViewCell
let people: Userx
people = people[indexPath.row]
cell.lblName.text = people.Education
cell.lblgenre.text = people.WhatIamConsideringBuying
return cell
}
@IBOutlet weak var table: UITableView!
var locationManager = CLLocationManager()
override func viewDidLoad() {
navigationItem.leftBarButtonItem = UIBarButtonItem(title: "Sign Out", style: .plain, target: self, action: #selector(signOut))
super.viewDidLoad()
let databaseRef = Database.database().reference()
databaseRef.child("Education").observe(DataEventType.value, with: {snapshot in
if snapshot.childrenCount>0{
self.people.removeAll()
for people in snapshot.children.allObjects as! [DataSnapshot] {
let peopleObject = people.value as? [String: AnyObject]
let peopleEducation = peopleObject?["Education"]
let peopleWhatIamConsideringBuying = peopleObject?["WhatIamConsideringBuying"]
let peoplePhotoPosts = peopleObject?["PhotoPosts"]
let people = Userx(Education: peopleEducation as! String?, WhatIamConsideringBuying: peopleWhatIamConsideringBuying as! String?, PhotoPosts: peoplePhotoPosts as AnyObject)
self.people.append(people)
}
self.table.reloadData()
}
})
//以下是另一个文件中的Userx:
class Userx {
var Education: String?
var WhatIamConsideringBuying: String?
var PhotoPosts: AnyObject?
init(Education: String?, WhatIamConsideringBuying: String?, PhotoPosts: AnyObject? ){
self.Education = Education
self.WhatIamConsideringBuying = WhatIamConsideringBuying
self.PhotoPosts = PhotoPosts
}
完成后,我希望获取并显示firebase数据。问题是您使用另一种类型的局部变量来覆盖实例属性
people
,即Userx
,而不是cellForRowAt
中的[Userx]
。应该为局部变量指定另一个名称
public override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
as! ViewControllerTableViewCell
let person: Userx = people[indexPath.row]
cell.lblName.text = person.Education
cell.lblgenre.text = person.WhatIamConsideringBuying
return cell
}
与您的问题无关,但您还应该遵守Swift命名约定,该约定对于变量名来说是小写的,并尽可能使用不可变的非可选值。使用struct
而不是class
还可以提供一个自动合成的成员初始值设定项,因此您不需要自己创建一个。当您知道变量应包含的类型时(大多数情况下都是这样),也应尽量不要使用Any
或AnyObject
问题是您使用另一种类型的局部变量
Userx
而不是cellForRowAt
中的[Userx]
覆盖实例属性people
。应该为局部变量指定另一个名称
public override func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell {
let cell = tableView.dequeueReusableCell(withIdentifier: "cell", for: indexPath)
as! ViewControllerTableViewCell
let person: Userx = people[indexPath.row]
cell.lblName.text = person.Education
cell.lblgenre.text = person.WhatIamConsideringBuying
return cell
}
与您的问题无关,但您还应该遵守Swift命名约定,该约定对于变量名来说是小写的,并尽可能使用不可变的非可选值。使用struct
而不是class
还可以提供一个自动合成的成员初始值设定项,因此您不需要自己创建一个。当您知道变量应包含的类型时(大多数情况下都是这样),也应尽量不要使用Any
或AnyObject
您在init中丢失了此文件中的一个大括号 您的代码
class Userx {
var Education: String?
var WhatIamConsideringBuying: String?
var PhotoPosts: AnyObject?
init(Education: String?, WhatIamConsideringBuying: String?, PhotoPosts: AnyObject? ){
self.Education = Education
self.WhatIamConsideringBuying = WhatIamConsideringBuying
self.PhotoPosts = PhotoPosts
}
固定代码
class Userx {
var Education: String?
var WhatIamConsideringBuying: String?
var PhotoPosts: AnyObject?
init(Education: String?, WhatIamConsideringBuying: String?, PhotoPosts: AnyObject? ){
self.Education = Education
self.WhatIamConsideringBuying = WhatIamConsideringBuying
self.PhotoPosts = PhotoPosts
}
}
您在init中丢失了此文件中的一个大括号 您的代码
class Userx {
var Education: String?
var WhatIamConsideringBuying: String?
var PhotoPosts: AnyObject?
init(Education: String?, WhatIamConsideringBuying: String?, PhotoPosts: AnyObject? ){
self.Education = Education
self.WhatIamConsideringBuying = WhatIamConsideringBuying
self.PhotoPosts = PhotoPosts
}
固定代码
class Userx {
var Education: String?
var WhatIamConsideringBuying: String?
var PhotoPosts: AnyObject?
init(Education: String?, WhatIamConsideringBuying: String?, PhotoPosts: AnyObject? ){
self.Education = Education
self.WhatIamConsideringBuying = WhatIamConsideringBuying
self.PhotoPosts = PhotoPosts
}
}
这很可能是复制粘贴错误,即使不是,也绝对不是特定编译时错误的原因。这很可能是复制粘贴错误,即使不是,也绝对不是特定编译时错误的原因。谢谢你的回答。这是有道理的。我仍然收到一个错误AppDelegate不符合协议UITableViewDataSource。我以前从未遇到过应用程序委托错误。你知道可能是什么原因吗?@user9269956该错误似乎与你问题中的代码无关。你应该问一个关于另一个问题的新问题。谢谢,谢谢你的回答。这是有道理的。我仍然收到一个错误AppDelegate不符合协议UITableViewDataSource。我以前从未遇到过应用程序委托错误。你知道可能是什么原因吗?@user9269956该错误似乎与你问题中的代码无关。你应该问一个关于另一个问题的新问题。谢谢