Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
924 views
in Technique[技术] by (71.8m points)

swift - I keep getting a SIGABRT error when trying to read my firebase database and create an array of objects with its contents. What am I doing wrong?

I did all of the SDK stuff that firebase told me to do like added their plist file and doing stuff in my AppDelegate.swift file. Sadly, it crashed every-time. What I want is my database data to be put into an array of Repo objects.

Image of firebase: https://i.stack.imgur.com/kfS0v.png

My error:

2020-02-17 19:11:44.788063-0600 RepoFinder[23102:1025340] *** Terminating app due to uncaught exception 'NSUnknownKeyException', reason: '[<FIRDataSnapshot 0x600003b8c9a0> valueForUndefinedKey:]: this class is not key value coding-compliant for the key name.'
*** First throw call stack:
(
    0   CoreFoundation                      0x00007fff23c7127e __exceptionPreprocess + 350
    1   libobjc.A.dylib                     0x00007fff513fbb20 objc_exception_throw + 48
    2   CoreFoundation                      0x00007fff23c70e49 -[NSException raise] + 9
    3   Foundation                          0x00007fff25713f47 -[NSObject(NSKeyValueCoding) valueForUndefinedKey:] + 225
    4   Foundation                          0x00007fff25712bba -[NSObject(NSKeyValueCoding) valueForKey:] + 317
    5   RepoFinder                          0x00000001043f0845 $s10RepoFinder15ContactFirebaseC04makeA5ArraySayAA0A0CGyFySo15FIRDataSnapshotCcfU_ + 261
    6   RepoFinder                          0x00000001043f0e26 $sSo15FIRDataSnapshotCIegg_ABIeyBy_TR + 70
    7   RepoFinder                          0x00000001044395f6 __63-[FIRDatabaseQuery observeEventType:withBlock:withCancelBlock:]_block_invoke + 118
    8   RepoFinder                          0x000000010441a7fc __43-[FChildEventRegistration fireEvent:queue:]_block_invoke.66 + 124
    9   libdispatch.dylib                   0x0000000105333dd4 _dispatch_call_block_and_release + 12
    10  libdispatch.dylib                   0x0000000105334d48 _dispatch_client_callout + 8
    11  libdispatch.dylib                   0x0000000105342de6 _dispatch_main_queue_callback_4CF + 1500
    12  CoreFoundation                      0x00007fff23bd4049 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE__ + 9
    13  CoreFoundation                      0x00007fff23bceca9 __CFRunLoopRun + 2329
    14  CoreFoundation                      0x00007fff23bce066 CFRunLoopRunSpecific + 438
    15  GraphicsServices                    0x00007fff384c0bb0 GSEventRunModal + 65
    16  UIKitCore                           0x00007fff48092d4d UIApplicationMain + 1621
    17  RepoFinder                          0x00000001043da0db main + 75
    18  libdyld.dylib                       0x00007fff5227ec25 start + 1
)
libc++abi.dylib: terminating with uncaught exception of type NSException
(lldb) 

Code for my Repo class:

import UIKit

class Repo {

    var name: String = ""
    var icon: String = ""
    var url: String = ""

}

And lastly my code for attempting to make the repo object array with the Firebase database data:

import UIKit
import Firebase
import FirebaseDatabase

class ContactFirebase {

    var repoArray = [Repo]()

    func makeRepoArray() -> [Repo] {


        let databaseRef = Database.database().reference()
        databaseRef.child("repos").queryOrderedByValue().observe(.childAdded) { (snapshot) in

            let repo = Repo()

            let name = snapshot.value(forKey: "name")
            let url = snapshot.value(forKey: "url")

            repo.name = name as! String
            repo.url = url as! String

            self.repoArray.append(repo)



        }
        return repoArray
    }
}

Thank you :)

See Question&Answers more detail:os

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)
Waitting for answers

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...