Firebase与Swift模糊使用observeEventType [英] Firebase with Swift ambiguous use of observeEventType

查看:165
本文介绍了Firebase与Swift模糊使用observeEventType的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

因为这个,我一直拉我的头发。去所有与相关事件和多个教程的页面,我发现我的代码在这里没有任何问题,但不知何故,只是不会失败,如果我打印出的价值(工程)或分配他们! NSArray然后给我一个空的数组。

打印的snapshot.value显示

 (friend1,
friend2,
friend3



我已经尝试了snapshot.value.values ...没有骰子。
我试着玩弄解开...没有骰子。
这是我的最后一次尝试:
$ b $ pre $ friendsList = [String]()

ref.observeSingleEventOfType(.Value){(snapshot) - >如果snapshot.value是NSNull {

} else {
快照中的子元素{
self.friendsList.append(child.value)
}
}
}

这再一次给我带来了模棱两可的感觉。 / b>

解决方案

无效


$ b 更改:快照中的子,因为快照不是一个序列,而snapshot.children是

我假设你想要将朋友名称存储为一个字符串,而 name 是结构中的一个键。因此,将 self.friendsList.append(child.value )更改为

  let name = child.value [名字]为?字符串
friendsList.append(name!)

以下是更正的代码:



pre $ var $ friends $ list



$ ref $ ob $
$ b如果snapshot.value是NSNull {

} else {
在snapshot.children中的子元素{
let name = child.value [name] as?String
friendsList.append(name!)

print(\(friendsList))
}
})


I've been pulling my hair out because of this. Going to all the pages with related incidents and multiple tutorials I find nothing wrong with my code here, but somehow it only doesn't fail if I print out the values (which works) or assign them as! NSArray which then gives me an empty array.

a print of snapshot.value shows

( friend1, 
  friend2, 
  friend3 
)

I've tried snapshot.value.values ... no dice. I've tried playing with the unwrapping ... no dice. and this is my final attempt:

    friendsList = [String]()

    ref.observeSingleEventOfType(.Value) { (snapshot) -> Void in
        if snapshot.value is NSNull {

        } else {
            for child in snapshot {
                self.friendsList.append(child.value)
            }
        }
    }

which gives me the ambiguous thing again.

解决方案

Just some coding errors

Remove: (snapshot)-> Void

Change: child in snapshot as snapshot is not a sequence, whereas snapshot.children is

I assume you want to store the friends name as a string and name is a key in your structure. So change self.friendsList.append(child.value) to

let name = child.value["name"] as? String
friendsList.append(name!)

Here's the corrected code:

var friendsList = [String]()

ref.observeSingleEventOfType(.Value, withBlock: { snapshot in

    if snapshot.value is NSNull {

    } else {
        for child in snapshot.children {
            let name = child.value["name"] as? String
            friendsList.append(name!)
        }
        print("\(friendsList)")
    }
})

这篇关于Firebase与Swift模糊使用observeEventType的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆