Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/16.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
Ios 如何从Firebase检索嵌套数据??应用程序正在崩溃_Ios_Swift_Firebase_Firebase Realtime Database_Firebase Storage - Fatal编程技术网

Ios 如何从Firebase检索嵌套数据??应用程序正在崩溃

Ios 如何从Firebase检索嵌套数据??应用程序正在崩溃,ios,swift,firebase,firebase-realtime-database,firebase-storage,Ios,Swift,Firebase,Firebase Realtime Database,Firebase Storage,我的Firebase存储看起来像这样 我可以在“兴趣”下保存,但当我试图检索用户id下的所有数据时,应用程序崩溃。我是Firebase的新手,经验不足,不知道如何调用所有数据(所有信息:电子邮件、位置、姓名等以及感兴趣的孩子) 有人能帮我吗?这是我的代码: func fetchUser() { guard let uid = FIRAuth.auth()?.currentUser?.uid else { //for some reason uid = nil

我的Firebase存储看起来像这样

我可以在“兴趣”下保存,但当我试图检索用户id下的所有数据时,应用程序崩溃。我是Firebase的新手,经验不足,不知道如何调用所有数据(所有信息:电子邮件、位置、姓名等以及感兴趣的孩子)

有人能帮我吗?这是我的代码:

 func fetchUser() {
    guard let uid = FIRAuth.auth()?.currentUser?.uid else {
        //for some reason uid = nil
        return
    }

    FIRDatabase.database().reference().child("users").child(uid).observeSingleEventOfType(.Value, withBlock: { (snapshot) in

        if let dictionary = snapshot.value as? [String: AnyObject] {
            //                self.navigationItem.title = dictionary["name"] as? String

            let user = User()
            user.setValuesForKeysWithDictionary(dictionary)
            self.setUpProfileInformationWithUser(user)
        }

        }, withCancelBlock: nil)
}


您的应用程序正在崩溃,原因是
用户。setValuesForKeysWithDictionary(dictionary)
。在类定义中,
interest
只是另一个属性,但当您从Firebase检索它时,它实际上是一个字典,因此编译器无法分别映射它。我可以很快想到两个选择

  • interest
    更改为类定义中的词典
  • 按如下所示的老方法提取数据

    FIRDatabase.database().reference().child("users").child(uid).observeSingleEventOfType(.Value, withBlock: { (snapshot) in
    
    if let dictionary = snapshot.value as? [String: AnyObject] {
        //                self.navigationItem.title = dictionary["name"] as? String
    
        guard let email = dictionary["email"] as! String, location = dictionary["location"] as! String, name = dictionary["name"] as! String, imageURL = dictionary["profileImageUrl"] as! String, description = dictionary["userDescription"] as! String, userName = dictionary["username"] as! String, interestDic = dictionary["interest"] as! [String : AnyObject] else
        {
            print("Error extracting FrB data")
            return
        }
    
        let interest = interestDic["interest"] as String
    
        let user = User(email, location, imageURL, description, userName, interest)
    
        self.setUpProfileInformationWithUser(user)
    }
    

  • 免责声明:我个人没有尝试过第一个选项,但值得一试。

    你能发布你的应用程序崩溃堆栈跟踪吗?谢谢你的回复,我尝试了这两个选项好几次,但它说用户不能接受参数,所以我不确定如何修复。你能发布你得到的确切错误吗?这可能是因为您没有构造函数,因此需要为每个新创建的对象分配所有属性。当我放入代码时,出现了许多错误,但主要错误是:条件绑定的初始值设定项必须具有可选类型,而不是“[String:AnyObject]”,然后当我删除“guard”时,它带来了更多的错误。您可能想看看我刚才关于检索Firebase数据的回答。。overflow.com/a/39862631
        2016-11-20 12:50:53.999 Your Niche[34689:1654014] -[__NSDictionaryI                 length]: unrecognized selector sent to instance 0x7f8bbd00f620
        2016-11-20 12:50:54.052 Your Niche[34689:1654014] *** Terminating         app due to uncaught exception 'NSInvalidArgumentException', reason: '-        [__NSDictionaryI length]: unrecognized selector sent to instance                 0x7f8bbd00f620'
        *** First throw call stack:
        (
            0   CoreFoundation                      0x00000001055c9d85 __exceptionPreprocess + 165
    1   libobjc.A.dylib                     0x0000000107b5cdeb objc_exception_throw + 48
    2   CoreFoundation                      0x00000001055d2d3d -[NSObject(NSObject) doesNotRecognizeSelector:] + 205
    3   CoreFoundation                      0x0000000105518cfa ___forwarding___ + 970
    4   CoreFoundation                      0x00000001055188a8 _CF_forwarding_prep_0 + 120
    5   libswiftCore.dylib                  0x000000010810c0c8 _TTSf4g_d___TFSSCfT12_cocoaStringPs9AnyObject__SS + 120
    6   libswiftCore.dylib                  0x00000001080cc873 _TFSSCfT12_cocoaStringPs9AnyObject__SS + 19
    7   libswiftFoundation.dylib            0x0000000108485d70 _TF10Foundation24_convertNSStringToStringFGSqCSo8NSString_SS + 16
    8   Your Niche                          0x0000000103fb2f8b _TToFC10Your_Niche4Users8interestGSqSS_ + 75
    9   Foundation                          0x000000010615219b -[NSObject(NSKeyValueCoding) setValue:forKey:] + 288
    10  Foundation                          0x000000010619399e -[NSObject(NSKeyValueCoding) setValuesForKeysWithDictionary:] + 261
    11  Your Niche                          0x0000000103fbe790 _TFFC10Your_Niche21ProfileViewController9fetchUserFT_T_U_FCSo15FIRDataSnapshotT_ + 464
    12  Your Niche                          0x0000000103f9fc1c _TTRXFo_oCSo15FIRDataSnapshot_dT__XFdCb_dS__dT__ + 60
    13  Your Niche                          0x0000000104081431 __71-[FIRDatabaseQuery observeSingleEventOfType:withBlock:withCancelBlock:]_block_invoke + 37
    14  Your Niche                          0x0000000104081731 __92-[FIRDatabaseQuery observeSingleEventOfType:andPreviousSiblingKeyWithBlock:withCancelBlock:]_block_invoke + 110
    15  Your Niche                          0x00000001040a7dcf __43-[FChildEventRegistration fireEvent:queue:]_block_invoke.64 + 88
    16  libdispatch.dylib                   0x00000001085fbd9d _dispatch_call_block_and_release + 12
    17  libdispatch.dylib                   0x000000010861c3eb _dispatch_client_callout + 8
    18  libdispatch.dylib                   0x00000001086041ef _dispatch_main_queue_callback_4CF + 1738
    19  CoreFoundation                      0x00000001055230f9 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
    20  CoreFoundation                      0x00000001054e4b99 __CFRunLoopRun + 2073
    21  CoreFoundation                      0x00000001054e40f8 CFRunLoopRunSpecific + 488
    22  GraphicsServices                    0x0000000109403ad2 GSEventRunModal + 161
    23  UIKit                               0x00000001065a8f09 UIApplicationMain + 171
    24  Your Niche                          0x0000000103f857d2 main + 114
    25  libdyld.dylib                       0x000000010865092d start + 1
        )
        libc++abi.dylib: terminating with uncaught exception of type NSException
        (lldb) 
    
    FIRDatabase.database().reference().child("users").child(uid).observeSingleEventOfType(.Value, withBlock: { (snapshot) in
    
    if let dictionary = snapshot.value as? [String: AnyObject] {
        //                self.navigationItem.title = dictionary["name"] as? String
    
        guard let email = dictionary["email"] as! String, location = dictionary["location"] as! String, name = dictionary["name"] as! String, imageURL = dictionary["profileImageUrl"] as! String, description = dictionary["userDescription"] as! String, userName = dictionary["username"] as! String, interestDic = dictionary["interest"] as! [String : AnyObject] else
        {
            print("Error extracting FrB data")
            return
        }
    
        let interest = interestDic["interest"] as String
    
        let user = User(email, location, imageURL, description, userName, interest)
    
        self.setUpProfileInformationWithUser(user)
    }