Swift 类属性返回nil
我正在使用Swift 类属性返回nil,swift,class,firebase,firebase-realtime-database,properties,Swift,Class,Firebase,Firebase Realtime Database,Properties,我正在使用FirebaseDatabase来存储数据。我有一个简单的模型,可以将快照数据转换为后期。问题是,在观察了快照之后,名为postType的属性返回nil,我无法找出原因 class Post { var id : String? var title : String? var content : String? var source : String? var userUid :
FirebaseDatabase
来存储数据。我有一个简单的模型,可以将快照
数据转换为后期
。问题是,在观察了快照之后,名为postType
的属性返回nil
,我无法找出原因
class Post {
var id : String?
var title : String?
var content : String?
var source : String?
var userUid : String?
var type : PostType?
}
extension Post {
static func transformDataToImagePost (dictionary: [String : Any], key: String) -> Post {
let post = Post()
post.id = key
post.userUid = dictionary["userUid"] as? String
post.title = dictionary["title"] as? String
post.content = dictionary["content"] as? String
post.source = dictionary["source"] as? String
post.type = dictionary["postType"] as? PostType
return post
}
enum PostType: String {
case image = "image"
case gif = "gif"
case video = "video"
case text = "text"
case link = "link"
case audio = "audio"
case poll = "poll"
case chat = "chat"
case quote = "quote"
}
func observePost(withId id: String, completion: @escaping (Post) -> Void) {
REF_POSTS.child(id).observeSingleEvent(of: .value, with: {
snapshot in
if let dictionary = snapshot.value as? [String : Any] {
print(snapshot.value) // <- ["key":value, "postType": text, "key": value]
print(dictionary["postType"]) // <- returns text
let post = Post.transformDataToImagePost(dictionary: dictionary, key: snapshot.key)
print(post.type) // <- returns nil
completion(post)
}
})
}
class Post{
变量id:字符串?
变量标题:字符串?
变量内容:字符串?
变量源:字符串?
var userUid:String?
变量类型:PostType?
}
延长柱{
静态func transformDataToImagePost(字典:[字符串:任意],键:字符串)->Post{
让post=post()
post.id=key
post.userUid=字典[“userUid”]作为?字符串
post.title=字典[“title”]作为字符串
post.content=字典[“内容”]作为字符串
post.source=字典[“source”]作为字符串
post.type=字典[“postType”]作为?postType
回程站
}
枚举后类型:字符串{
case image=“image”
case gif=“gif”
case video=“视频”
case text=“text”
case link=“link”
case audio=“audio”
case poll=“poll”
case chat=“chat”
case quote=“quote”
}
func observePost(id:String,completion:@escaping(Post)->Void){
REF_POSTS.child(id).observeSingleEvent(of:.值,带:{
快照
如果let dictionary=snapshot.value as?[字符串:Any]{
print(snapshot.value)/Post.type
是一个enum
,您不能只将其设置为字符串。您所拥有的等价物是:Post.type=“text”
尝试以以下方式初始化它:
post.type = PostType(rawValue: dictionary["postType"])
您可能需要首先展开该值
if let postType = dictionary["postType"] as? String {
post.type = PostType(rawValue: postType)
}
如何在游乐场中初始化枚举的示例如下:
enum PostType: String {
case image = "image"
case gif = "gif"
case video = "video"
case text = "text"
case link = "link"
case audio = "audio"
case poll = "poll"
case chat = "chat"
case quote = "quote"
}
let postType = PostType(rawValue: "poll")
print(postType)
输出可选(\uuuulldb\uexpr\u339.PostType.poll)
您可以在中阅读有关枚举的更多信息。对于此特定问题,请查找标题:“从原始值初始化”Post.type
是一个enum
,您不能只将其设置为字符串。您所拥有的等同于:Post.type=“text”
尝试以以下方式初始化它:
post.type = PostType(rawValue: dictionary["postType"])
您可能需要首先展开该值
if let postType = dictionary["postType"] as? String {
post.type = PostType(rawValue: postType)
}
如何在游乐场中初始化枚举的示例如下:
enum PostType: String {
case image = "image"
case gif = "gif"
case video = "video"
case text = "text"
case link = "link"
case audio = "audio"
case poll = "poll"
case chat = "chat"
case quote = "quote"
}
let postType = PostType(rawValue: "poll")
print(postType)
输出可选(\uuuulldb\uexpr\u339.PostType.poll)
您可以在中阅读有关枚举的更多信息。对于此特定问题,请查找标题:“从原始值初始化”您确定postType
从Firebase套装返回时带有枚举吗?请发布更多代码以查看您所做的事情您确定postType
从Firebase套装返回时带有枚举吗?请发布更多代码以查看您所做的事情