2016-10-04 64 views
0

我很困惑,因爲這只是停止工作沒有地方。我不確定它是否與我的綁定有關,或者它們是否被棄用或者交易是什麼,但之前沒有問題,我沒有任何值爲零。我有它的工作很長一段時間,現在我一直在網上收到此錯誤:意外地發現零,同時展開一個可選的值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

是我的陣列控制器連接在接口 –

回答

-1

至少以下是可選的:

product["product_name"] 

可能應該使用

product["product_name"]! 

取而代之(取決於您的情況)。

+0

我試過了,並且出現錯誤:「無法強制展開非可選類型'JSON'的值」「 –

+0

您能否對產品[」product_name「]和產品[」product_name「 ]。字符串值? –

+0

是的,我可以,但最近我一直有問題。它似乎不再打印出我的結果。 –

相關問題