意外地发现零,同时展开一个可选的值JSON swift

问题描述:

我很困惑,因为这只是停止工作没有地方。我不确定它是否与我的绑定有关,或者它们是否被弃用或者交易是什么,但之前没有问题,我没有任何值为零。我有它的工作很长一段时间,现在我一直在网上收到此错误:意外地发现零,同时展开一个可选的值JSON swift

self.arrayControllerNames.addObject(names) 

Controller.swift

import Cocoa 

class Controller: NSObject { 

@IBOutlet weak var arrayControllerNames: NSArrayController! 

@IBOutlet weak var arrayControllerPrices: NSArrayController! 


var names: NSMutableArray = NSMutableArray() 
var prices: NSMutableArray = NSMutableArray() 
var descriptions: NSMutableArray = NSMutableArray() 
var images: NSMutableArray = NSMutableArray() 
var videos: NSMutableArray = NSMutableArray() 
var downloads: NSMutableArray = NSMutableArray() 


override func awakeFromNib() { 





    let url = NSURL(string: "http://url") 
    let request = NSURLRequest(url: url as! URL) 
    let session = URLSession(configuration: URLSessionConfiguration.default) 
    let task = session.dataTask(with: request as URLRequest) { (data,response,error) -> Void in 

     if error == nil { 

      let swiftyJSON = JSON(data:data!) 
      print(swiftyJSON) 
      let products = swiftyJSON[].arrayValue 

      for product in products{ 

       let names = product["product_name"].stringValue 

       //let prices = product["product_price"].stringValue 
       //let descriptions = product["product_description"].stringValue 
       //let images = product["product_description"].stringValue 
       //let videos = product["product_description"].stringValue 
       //let downloads = product["product_description"].stringValue 



        self.arrayControllerNames.addObject(names) 

       //self.arrayControllerPrices.addObject(prices) 
       //self.arrayControllerPrices.addObject(descriptions) 
       //self.arrayControllerPrices.addObject(images) 
       //self.arrayControllerPrices.addObject(videos) 
       //self.arrayControllerPrices.addObject(downloads) 
      } 


     } else{ 
      print("Error") 
     } 
    } 
    task.resume() 



} 
} 
+1

“NSArrayController”根本不能用多个数组作为数据源。使用自定义类作为模型。和**不要在Swift **中使用'NSMutableArray/NSMutableDictionary'。解决那些暗示这些教程。阵列控制器是否在Interface Builder中连接? – vadian

+0

是我的阵列控制器连接在接口 –

至少以下是可选的:

product["product_name"] 

可能应该使用

product["product_name"]! 

取而代之(取决于您的情况)。

+0

我试过了,并且出现错误:“无法强制展开非可选类型'JSON'的值”“ –

+0

您能否对产品[”product_name“]和产品[”product_name“ ]。字符串值? –

+0

是的,我可以,但最近我一直有问题。它似乎不再打印出我的结果。 –