2017-11-01 7 views
0

にここにgithubの上の私のプロジェクトです: https://github.com/alshfu86/Recipesは、型の値をキャストすることができませんでした「nsnullを」(0×106a4f520)「NSDictionaryの」

私は1つの問題があります。私は私のFirebaseデータベースから何かを削除した場合、私はこのエラーを取得する:方法で

Could not cast value of type ‘NSNull’ (0×106a4f520) to ‘NSDictionary’ (0×106a4ef58).

:ここ

func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
    let cell = tableView.dequeueReusableCell(withIdentifier: "CategoryCell", for:indexPath) as! CategoryTableViewCell 
    let rowData = self.data[indexPath.row] as! NSDictionary 
    let title = rowData["title"] as! String 
    cell.recipeLabel.text = title.uppercased() 
    let imageUrl = rowData["youtubeID"] as! String 
    let imageQuality = rowData["imageQuality"] as! String 
    let urlPath = youtubeBaseImageURL + imageUrl + "/" + imageQuality + ".jpg" 
    let url = URL(string: urlPath) 
    cell.recipeImageView.kf.setImage(with: url) 
    print("uString(describing: rl)path = \(String(describing: url))") 
    return cell 
} 

は、すべてのクラスである:

import UIKit 
import Kingfisher 

class CategoryViewController: UIViewController,UITableViewDataSource,UITableViewDelegate { 
    var data = [Any]() 
    var categoryTitle = "" 

    let youtubeBaseImageURL = "http://img.youtube.com/vi/" 

    @IBOutlet weak var tableView: UITableView! 

    //is the device landscape or portrait 
    var isPortraid = true 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     let backButton = UIBarButtonItem(image: UIImage(named:"chevron"), style: UIBarButtonItemStyle.plain, target: self, action: #selector(back)) 

     self.navigationItem.leftBarButtonItem = backButton 
     self.tableView.delegate = self 
     self.title = categoryTitle 

     print("data: \(self.data)") 

     // loadAdmobBanner() 

     NotificationCenter.default.addObserver(self, selector: #selector(MainViewController.orientationChanged), name: NSNotification.Name.UIDeviceOrientationDidChange, object: nil) 

     if UIDevice.current.orientation.isLandscape { 
      isPortraid = false 
     } 
    } 

    func orientationChanged(){ 
     if UIDevice.current.orientation.isLandscape { 
      isPortraid = false 
     } else { 
      isPortraid = true 
     } 

     tableView.reloadData() 
    } 

    @objc func back(){ 
     self.navigationController?.popViewController(animated: true) 
    } 

    func numberOfSections(in tableView: UITableView) -> Int { 
     return 1 
    } 

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

    func tableView(_ tableView: UITableView, cellForRowAt indexPath: IndexPath) -> UITableViewCell { 
     let cell = tableView.dequeueReusableCell(withIdentifier: "CategoryCell", for:indexPath) as! CategoryTableViewCell 
     let rowData = self.data[indexPath.row] as! NSDictionary 
     let title = rowData["title"] as! String 
     cell.recipeLabel.text = title.uppercased() 
     let imageUrl = rowData["youtubeID"] as! String 
     let imageQuality = rowData["imageQuality"] as! String 
     let urlPath = youtubeBaseImageURL + imageUrl + "/" + imageQuality + ".jpg" 
     let url = URL(string: urlPath) 
     cell.recipeImageView.kf.setImage(with: url) 
     print("uString(describing: rl)path = \(String(describing: url))") 
     return cell 
    } 

    func tableView(_ tableView: UITableView, didSelectRowAt indexPath: IndexPath) { 
     let storyboard = UIStoryboard(name: "Main", bundle: Bundle.main) 
     let playerViewController = storyboard.instantiateViewController(withIdentifier: "PlayerViewController") as! PlayerViewController 
     let rowData = self.data[indexPath.row] as! NSDictionary 
     let youtubeUrl = rowData["youtubeID"] as! String 
     playerViewController.urlPath = youtubeUrl 
     let ingredients = rowData["ingredients"] as! String 
     playerViewController.about = ingredients 

     self.navigationController?.pushViewController(playerViewController, animated: true) 
    } 

    func tableView(_ tableView: UITableView, heightForRowAt indexPath: IndexPath) -> CGFloat { 
     if isPortraid {return UIScreen.main.bounds.height/5 
     } else { 
      return UIScreen.main.bounds.height/5.2 
     } 
    } 

    @IBAction func unwindToCategory(sender: UIStoryboardSegue){ 
    } 
+0

エラーが発生するのは、どのラインですか? –

+0

オンライン:let rowData = self.data [indexPath.row]を! NSDictionary –

+0

'cellForRow'は、' null'データをフィルタリングする場所が間違っています。データの読み込み中にこれを行う必要があります。そして、いつものように、SwiftでNSDictionaryを使わないでください。 – vadian

答えて

0

エラーは、このライン上にある必要があります:

let rowData = self.data[indexPath.row] as! NSDictionary 

エラーまたは配列にヌル値(NSNullで表される)が含まれていることです。辞書の配列としてdataを宣言し、

if let rowData = self.data[indexPath.row] as? NSDictionary { 
    // do stuff here 
} 

または代わりに:あなたはおそらく、条件付きキャスト、すなわちを行う必要があります

var data = [NSDictionary]() 

その後、dataに割り当てる前にNULL値をフィルタリング:

self.data = yourdatasource.filter { $0 is NSDictionary } 

その後、単純にe礼儀:

let rowData = self.data[indexPath.row] // do stuff here 
+0

あなたは私にいくつかの元を与えることができますか?フィルターの? –

+0

私は例を追加しました。 – paulvs

+0

もし私が使用しようとすると、私はしようとするrowData = self.data [indexPath.row] { // do stuff here here }条件バインディングの初期化子は、 'NSDictionary'ではないオプションの型を持っている必要があります –

関連する問題