为什么我的模型不在视图控制器中显示其成员?
Why Isn't My Model Showing Its Member in View Controller?
我有一个名为 Profile 的模型,其中包含 2 个成员 posPoints
和 negPoints
。我正在尝试在名为 MyProfileViewController
的 VC 上显示当前用户的积分。但是,当我键入 profiles.posPoints
时,Xcode 无法识别它并给我这个错误
Value of type '[Profile]' has no member 'posPoints'
static func show(for user: User = User.current, completion: @escaping (Profile?) -> Void) {
let profileRef = Database.database().reference().child("profile").child(user.username)
let ref = Database.database().reference().child("profile").child(user.username).child(profileRef.key ?? "")
ref.observeSingleEvent(of: .value, with: { (snapshot) in
guard let profile = Profile(snapshot: snapshot) else {
return completion(nil)
}
completion(profile)
})
}
import Foundation
import FirebaseDatabase.FIRDataSnapshot
class Profile {
// MARK - Properties
var key: String?
let posPoints: Int
let negPoints: Int
init?(snapshot: DataSnapshot) {
guard !snapshot.key.isEmpty else {return nil}
if let dict = snapshot.value as? [String : Any]{
let posPoints = dict["posPoints"] as? Int
let negPoints = dict["negPoints"] as? Int
self.key = snapshot.key
self.posPoints = posPoints ?? 0
self.negPoints = negPoints ?? 0
}
else{
return nil
}
}
}
(在 MyProfileViewController 的 viewDidLoad 中)
ProfileService.show { [weak self] (profiles) in
self?.profiles = profiles
}
myPointsLabel.text = profiles.posPoints
}
Firebase 数据库视图
您在 viewDidLoad
中的代码存在以下问题。
1- 在 completionHandler
中更新 myPointsLabel
,其中获取 profiles
已完成。
2- UI 更新应该在主线程上完成。
3-首先从profiles
的array
中提取出想要的profile
,然后接入点并转化为String
.
您的代码应如下所示,
var profile: Profile?
override func viewDidLoad() {
super.viewDidLoad()
ProfileService.show { [weak self] (profile) in
self?.profile = profile
if let points = profile?.posPoints {
DispatchQueue.main.async {
self?.myPointsLabel.text = String(points)
}
}
}
}
我有一个名为 Profile 的模型,其中包含 2 个成员 posPoints
和 negPoints
。我正在尝试在名为 MyProfileViewController
的 VC 上显示当前用户的积分。但是,当我键入 profiles.posPoints
时,Xcode 无法识别它并给我这个错误
Value of type '[Profile]' has no member 'posPoints'
static func show(for user: User = User.current, completion: @escaping (Profile?) -> Void) {
let profileRef = Database.database().reference().child("profile").child(user.username)
let ref = Database.database().reference().child("profile").child(user.username).child(profileRef.key ?? "")
ref.observeSingleEvent(of: .value, with: { (snapshot) in
guard let profile = Profile(snapshot: snapshot) else {
return completion(nil)
}
completion(profile)
})
}
import Foundation
import FirebaseDatabase.FIRDataSnapshot
class Profile {
// MARK - Properties
var key: String?
let posPoints: Int
let negPoints: Int
init?(snapshot: DataSnapshot) {
guard !snapshot.key.isEmpty else {return nil}
if let dict = snapshot.value as? [String : Any]{
let posPoints = dict["posPoints"] as? Int
let negPoints = dict["negPoints"] as? Int
self.key = snapshot.key
self.posPoints = posPoints ?? 0
self.negPoints = negPoints ?? 0
}
else{
return nil
}
}
}
(在 MyProfileViewController 的 viewDidLoad 中)
ProfileService.show { [weak self] (profiles) in
self?.profiles = profiles
}
myPointsLabel.text = profiles.posPoints
}
Firebase 数据库视图
您在 viewDidLoad
中的代码存在以下问题。
1- 在 completionHandler
中更新 myPointsLabel
,其中获取 profiles
已完成。
2- UI 更新应该在主线程上完成。
3-首先从profiles
的array
中提取出想要的profile
,然后接入点并转化为String
.
您的代码应如下所示,
var profile: Profile?
override func viewDidLoad() {
super.viewDidLoad()
ProfileService.show { [weak self] (profile) in
self?.profile = profile
if let points = profile?.posPoints {
DispatchQueue.main.async {
self?.myPointsLabel.text = String(points)
}
}
}
}