2017-04-06 24 views
0

嗨,大家好,我陷入了一個問題,無法找到解決方案。任何人都可以告訴我最新的問題,我剛開始學習迅速我想從我的數據火力數據庫,並打印在UITableViewCell的從UiTableViewCell中的單元格中的Firebase獲取沒有數據Swift 3

我創建了具有

類分類迅速文件:NSObject的{VAR 類別:字符串? }

我的viewController低於

import UIKit 
import FirebaseDatabase 


class ViewController: UIViewController, UITableViewDataSource, UITableViewDelegate { 

    var categoryList = [Categories]() 


    var refHandle:UInt! 
    var ref:FIRDatabaseReference? 

    @IBOutlet weak var listOfCategories: UITableView! 
    @IBOutlet weak var menuBtn: UIBarButtonItem! 


    override func viewDidLoad() { 
     super.viewDidLoad() 

     ref = FIRDatabase.database().reference() 

     menuBtn.target = revealViewController() 
     menuBtn.action = #selector(SWRevealViewController.revealToggle(_:)) 

     fetchCategories() 
    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    func tableView(_ tableView: UITableView, numberOfRowsInSection section: Int) -> Int { 
     return categoryList.count 
    } 


    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = UITableViewCell(style: .default, reuseIdentifier: "cell") 

     // Set cell content 
     cell.textLabel?.text = categoryList[indexPath.row].categories 

    return cell 


    } 

    func fetchCategories(){ 

     refHandle = ref?.child("Categories").observe(.childAdded, with: { (snapshot) in 

      if let dictionary = snapshot.value as? [String: AnyObject] { 

       print(dictionary) 

       let categories = Categories() 



       categories.setValuesForKeys(dictionary) 
       self.categoryList.append(categories) 

       DispatchQueue.main.async 
        { 
        self.listOfCategories.reloadData() 
       } 

      } 
     }) 

    } 


} 

我的數據庫

-Categories 
    -name 
    Bikes:"Bikes" 
    Cars:"Cars" 
    Cats:"Cats" 

如果我打印(categorList)我不明白它的控制檯上也..下面是日誌

2017-04-06 06:09:50.531壁紙應用[1716] [Firebase/Analytics] [I-ACS023007] Firebase A 09:v.3700000開始 2017年4月6日06 nalytics 50.534壁紙應用[1716] [火力地堡/ Analytics(分析)] [I-ACS023008]啓用調試日誌記錄設置 以下應用參數:-FIRAnalyticsDebugEnabled(見 http://goo。 2017-04-06 06:09:50.565壁紙應用[1716] [Firebase/Analytics] [I-ACS003007]自動成功創建 Firebase Analytics應用委託代理。要禁用 代理,請在 Info.plist 2017-04-06 06:09:50.580壁紙應用中設置標誌FirebaseAppDelegateProxyEnabled爲NO [1716] [Firebase/Analytics] [I-ACS005000] AdSupport框架不是 目前已鏈接。某些功能將無法正常工作。瞭解更多 在http://goo。 gl/9vSsPb 2017-04-06 06:09:50.589壁紙應用[1716] [Firebase/Analytics] [I-ACS023012]啓用Firebase Analytics [「汽車」:汽車,「自行車」:自行車,「貓」:貓] 2017年4月6日06:09:52.565 壁紙應用[1716:28745] *終止應用程序由於未捕獲的異常 'NSUnknownKeyException',原因是: 「[ 的setValue:forUndefinedKey:]:此類不是關鍵值爲 編碼符合關鍵汽車。' *第一擲調用堆棧:(0的CoreFoundation 0x0000000104725d4b exceptionPreprocess + 171 1 libobjc.A.dylib
0x0000000103d6621e objc_exception_throw + 48 2的CoreFoundation
0x0000000104725c99 - [NSException提高] + 9 3基金會
0x00000001038749df - [NSObject的(的NSKeyValueCoding )的setValue:forKey:] + 291 4基金會0x00000001038d672f - [NSObject的(的NSKeyValueCoding)setValuesForKeysWithDictionary:] + 301 5壁紙應用0x00000001026fe886 _TFFC14Wallpapers_App14ViewController15fetchCategoriesFT_T_U_FCSo15FIRDataSnapshotT_ + 1126 6壁紙應用0x00000001026fed8c _TTRXFo_oCSo 15FIRDataSnapshot__XFdCb_dS_
+ 60 7壁紙應用0x00000001027c85b0 63- [FIRDatabaseQuery observeEventType:withBlock:withCancelBlock:] _ block_invoke + 37 8
壁紙應用0x00000001027efeb3 __43- [FChildEventRegistration fireEvent:隊列:] _ block_invoke.68 + 88 9 libdispatch。dylib 0x0000000107470978 _dispatch_call_block_and_release + 12個10 libdispatch.dylib 0x000000010749a0cd _dispatch_client_callout + 8 11 libdispatch.dylib 0x000000010747a8a4 _dispatch_main_queue_callback_4CF + 406 12 的CoreFoundation 0x00000001046e9e49 __CFRUNLOOP_IS_SERVICING_THE_MAIN_DISPATCH_QUEUE
+ 9 13的CoreFoundation 0x00000001046af37d __CFRunLoopRun + 2205 14的CoreFoundation 0x00000001046ae884 CFRunLoopRunSpecific + 420個15 GraphicsServices
0x0000000109439a6f GSEventRunModal + 161 16 UIKit
0x000000010525cc68 UIApplicationMain + 159 17壁紙App
0x0000000102701fcf main + 111 18 libdyld.dylib
0x00000001074e668d啓動+ 1)的libC++ abi.dylib:與類型NSException(LLDB的 未捕獲的異常)

回答

0

您正在使用方法

setValuesForKeys 

此方法描述終止說:

Sets properties of the receiver with values from a given dictionary, using its keys to identify the properties. 

而你的錯誤說,你的類別不是與鍵「汽車」的關鍵值兼容,因爲沒有鑰匙/道具在你的「類別」類中命名爲「汽車」。

從我看到的,你試圖在UITableViewCell中打印所有這些類別。我建議將您的類別類更改爲以下內容:

struct Category() { 
    var key: String 
    var value: String 
} 

發佈說明,當您在fetchCategories()方法中獲得響應時。解析該字典:

for (key, value) in dictionary { 
    let category = Category(key: key, value: value as! String) 
    self.listOfCategories.append(category) 
} 

在您的cellForRowAtIndexPath中。你可以像這樣設置文本。

cell.textLabel?.text = categoryList[indexPath.row].value 

如果您只需要從Firebase收到的響應中的值,則可能需要簡化類別結構。然後,你的結構就變得非常簡單:

struct Category() { 
    var name: String 
} 

我更喜歡使用過類結構的地方我可以,但你可以使用一個類,如果你真正想要的。

0

看起來像你對我有兩個問題 1),顯示在小區 2中的數據)的打印數據,所以你可以看到它

我會地址2),因爲我認爲一旦你看到的數據,你可以使用它(至少這是我的經驗 - 也被相當新的Xcode)

Xcode不是「簡單」的打印數據,如我們想......(你可能會發現它是令人沮喪,因爲我已經 - 從JS/jQuery的/ PHP背景的)

我覺得最簡單的什麼是用一種稱爲「簡體插件leJSON」(還有其他的,這是我找到適合我的目的之一),並採取數據到JSON包,在那裏我可以看到數據(整體思路,對吧?),然後就可以從有需要的工作。

在POD文件

,只需添加

import SwiftyJSON 

然後在你的代碼,你想看到的數據

let categoriesJSON = JSON(categories) 
print(categoriesJSON) 

從那裏,你會得到一個列表中的數組一起工作

print(categoriesJSON["name"]["Cars"] 

不是一個完全的'Swift-y'解決方案,但它可以完成工作!

相關問題