Warning: file_get_contents(/data/phpspider/zhask/data//catemap/3/arrays/12.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/18.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Arrays 从firebase读取数组一次并将其放入数组中_Arrays_Swift_Firebase Realtime Database - Fatal编程技术网

Arrays 从firebase读取数组一次并将其放入数组中

Arrays 从firebase读取数组一次并将其放入数组中,arrays,swift,firebase-realtime-database,Arrays,Swift,Firebase Realtime Database,如何读取firebase阵列并将其放入swift阵列?我花了4个小时来解决这个问题。我做错了什么 您正试图将一个字符串数组作为单个字符串展开,这就是它失败的原因。更改为: ref.child("names").child("myNames").observe(.value) { (snapshot) in if let item = snapshot.value as? [String] { namesArray = item } } 您正试图将一个字符串数组

如何读取firebase阵列并将其放入swift阵列?我花了4个小时来解决这个问题。我做错了什么


您正试图将一个
字符串
数组作为单个
字符串
展开,这就是它失败的原因。更改为:

ref.child("names").child("myNames").observe(.value) { (snapshot) in
    if let item = snapshot.value as? [String] {
        namesArray = item
    }
}

您正试图将一个
字符串
数组作为单个
字符串
展开,这就是它失败的原因。更改为:

ref.child("names").child("myNames").observe(.value) { (snapshot) in
    if let item = snapshot.value as? [String] {
        namesArray = item
    }
}

您应该将快照解析为
[String:Any]?
,并从字典中获取值

ref.child("names").child("myNames").observe(.value) { (snapshot) in
    if let itemDictionary = snapshot.value as? [String : Any] {
        for (key, value) in itemDictionary {

            // Another check for String
            if let valueString = value as? String {
                namesArray.append(valueString)
            } 
        }
    }
}

您应该将快照解析为
[String:Any]?
,并从字典中获取值

ref.child("names").child("myNames").observe(.value) { (snapshot) in
    if let itemDictionary = snapshot.value as? [String : Any] {
        for (key, value) in itemDictionary {

            // Another check for String
            if let valueString = value as? String {
                namesArray.append(valueString)
            } 
        }
    }
}

代码看起来不错。有什么问题吗?不应该
as吗?字符串
be
as?[String:Any]
?@FrankvanPuffelen数组保持为空,即使我等待一分钟并使用button@EDUsta如果我将其更改为
as?[String:Any]
我收到一个错误:无法将“[String:Any]”类型的值转换为预期的参数类型“String”@articga您应该迭代字典并将值推送到namesArray。那是[字符串],对吗?那代码看起来不错。有什么问题吗?不应该
as吗?字符串
be
as?[String:Any]
?@FrankvanPuffelen数组保持为空,即使我等待一分钟并使用button@EDUsta如果我将其更改为
as?[String:Any]
我收到一个错误:无法将“[String:Any]”类型的值转换为预期的参数类型“String”@articga您应该迭代字典并将值推送到namesArray。那是[字符串],对吗?