Warning: file_get_contents(/data/phpspider/zhask/data//catemap/8/swift/20.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
在展开可选值JSON swift时意外发现nil_Json_Swift_Macos - Fatal编程技术网

在展开可选值JSON swift时意外发现nil

在展开可选值JSON swift时意外发现nil,json,swift,macos,Json,Swift,Macos,我很困惑,因为这只是停止工作,没有地方。我不确定这是否与我的绑定有关,或者它们是否已被弃用,或者交易是什么,但我以前没有任何问题,也没有零值。我让它工作了很长一段时间,现在我不断在网上发现这个错误: self.arrayControllerNames.addObject(names) 控制器。swift import Cocoa class Controller: NSObject { @IBOutlet weak var arrayControllerNames: NSArrayCont

我很困惑,因为这只是停止工作,没有地方。我不确定这是否与我的绑定有关,或者它们是否已被弃用,或者交易是什么,但我以前没有任何问题,也没有零值。我让它工作了很长一段时间,现在我不断在网上发现这个错误:

self.arrayControllerNames.addObject(names)
控制器。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()



}
}

至少以下选项是可选的:

product["product_name"]
可能应该使用

product["product_name"]!

相反(取决于您的上下文)。

NSArrayController
对于作为数据源的多个阵列根本不起作用。使用自定义类作为模型。不要在Swift中使用
NSMutableArray/NSMutableDictionary
。起诉那些建议这样做的教程。阵列控制器是否在Interface Builder中连接?是的,我的阵列控制器已在Interface Builder中连接。我尝试了此操作,但出现错误:“无法强制展开非可选类型“JSON”的值”。您可以订购产品[“product_name”]和产品[“product_name”]。stringValue吗?是的,我可以,但最近一直有问题。它似乎不再打印我的结果了。请不要建议人们强制打开东西,永远不要。曾经永远@Grimxn,谢谢你的意见。我只是想帮忙。我想我确实说过“可能”和“取决于你的背景”。