输入'Any?'没有下标成员(使用Firebase) [英] type 'Any?' has no subscript members (using Firebase)

查看:69
本文介绍了输入'Any?'没有下标成员(使用Firebase)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

每次我运行这行代码都不起作用,任何能够帮助我做我能做什么的人都会改变它?谢谢你的帮助。 :)

Every time I run this line of code it doesn't work, anyone who can give me a hand in what I can do too change it? Thanks for any help. :)

以下是我一直得到的错误

Below is the error I keep getting


Type Any?没有下标成员

Type Any? has no subscript members



var ref:FIRDatabaseReference?
var refHandle: UInt!


var postData = [String]()

override func viewDidLoad() {

    super.viewDidLoad()



    ref = FIRDatabase.database().reference()
    refHandle = ref?.observe(FIRDataEventType.value, with:
    { (snapshot) in

        let dataDict = snapshot.value as! [String: AnyObject]

        print(dataDict)


    })

    let username: String = (FIRAuth.auth()?.currentUser?.uid)!

    ref?.child("Users").child(username).observeSingleEvent(of: .value, with:
    { (snapshot) in
        let username = snapshot.value!["Username"] as! String

        self.usernameField.text = username


    })

}


推荐答案

两个问题。

1。可选项

这是Swift使变量处于两种状态之一的方式,即具有值或 nil 。变量只能处于其中一种状态。您可以通过在其前面添加问号来使变量成为可选项。

This is Swift's way of making a variable be in one of two states namely, have a value or be nil. A variable can only be in one of these states. You make a variable an optional by adding a question mark in front of it.

2。任何

通过声明变量类型任何,这意味着你没有明确在声明期间陈述其类型。 Firebase的所有返回值都是任何类型,以便我们开发人员可以选择摆弄数据,因为我们这样做了更少的约束。

By declaring a variable to be of type Any, this means you're not explicitly stating its type during declaration. Firebase makes all of its returns be of type Any in order to give us developers the option of fiddling with the data as we so please hence less constraints on our side.

snapshot.value 的类型为任何但Firebase始终返回JSON树和JSON树可以表示为 Dictionaries 。那么我们该怎么办?

snapshot.value is of type Any but Firebase always returns a JSON tree and JSON trees can be represented as Dictionaries. So what should we do?


  1. 由于 snapshot.value 是可选的,我们应首先检查它是否 nil

  2. 如果不是 nil ,将其转换为字典,然后开始访问其中的相应元素。

  1. Since snapshot.value is an optional, we should check first to see if its nil.
  2. If not nil, convert it to a dictionary then start accessing the respective elements inside it.

以下代码为您完成工作,我已添加评论来解释发生了什么。

The code below does the job for you and I've added comments to explain what's happening.

ref?.child("Users").child(username).observeSingleEvent(of: .value, with:
{ (snapshot) in

    // This does two things.
    // It first checks to see if snapshot.value is nil. If it is nil, then it goes inside the else statement then prints out the statement and stops execution.
    // If it isn't nil though, it converts it into a dictionary that maps a String as its key and the value of type AnyObject then stores this dictionary into the variable firebaseResponse.

    // I used [String:Any] because this will handle all types of data types. So your data can be Int, String, Double and even Arrays.
    guard let firebaseResponse = snapshot.value as? [String:Any] else
    {
        print("Snapshot is nil hence no data returned")
        return
    }

    // At this point we just convert the respective element back to its proper data type i.e from AnyObject to say String or Int etc

    let userName = firebaseResponse["Username"] as! String

    self.usernameField.text = username     
})

这篇关于输入'Any?'没有下标成员(使用Firebase)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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