Swift 需要将NSJSONSerialization调整为iOS10

Swift 需要将NSJSONSerialization调整为iOS10,swift,ios10,Swift,Ios10,升级到iOS10后,用户开始抱怨我的应用程序崩溃。 我正在模拟器上用iOS10测试它,事实上,应用程序崩溃时会显示一条消息:“无法将“\uu NSArrayI”类型的值强制转换为“NSMutableArray”。这是我的代码,请帮助: import Foundation protocol getAllListsModel: class { func listsDownloadingComplete(downloadedLists: [ContactsList]) } class Li

升级到iOS10后,用户开始抱怨我的应用程序崩溃。 我正在模拟器上用iOS10测试它,事实上,应用程序崩溃时会显示一条消息:“无法将“\uu NSArrayI”类型的值强制转换为“NSMutableArray”。这是我的代码,请帮助:

import Foundation

protocol getAllListsModel: class {
    func listsDownloadingComplete(downloadedLists: [ContactsList])
}

class ListsDownloader: NSObject, NSURLSessionDataDelegate{

    //properties

    weak var delegate: getAllListsModel!

    var data : NSMutableData = NSMutableData()

    func downloadLists() {

        let urlPath: String = "http://..."
        let url: NSURL = NSURL(string: urlPath)!
        var session: NSURLSession!
        let configuration =     NSURLSessionConfiguration.ephemeralSessionConfiguration()     //defaultSessionConfiguration()


    session = NSURLSession(configuration: configuration, delegate: self, delegateQueue: nil)

    let task = session.dataTaskWithURL(url)

    task.resume()

}

func URLSession(session: NSURLSession, dataTask: NSURLSessionDataTask, didReceiveData data: NSData) {
    self.data.appendData(data);
}

func URLSession(session: NSURLSession, task: NSURLSessionTask, didCompleteWithError error: NSError?) {
    if error != nil {
        print("Failed to download data")
    }else {
        self.parseJSON()
        print("Lists downloaded")
    }

}
func parseJSON() {

    var jsonResult: NSMutableArray = NSMutableArray()

    do{
        try jsonResult =  NSJSONSerialization.JSONObjectWithData(self.data, options:NSJSONReadingOptions.AllowFragments) as! NSMutableArray

        } catch let error as NSError {
        print(error)
        }

        var jsonElement: NSDictionary = NSDictionary()
        var downloadedLists: [ContactsList] = []

        for i in 0...jsonResult.count-1 {

            jsonElement = jsonResult[i] as! NSDictionary

            let tempContactsList = ContactsList()

            //the following insures none of the JsonElement values are nil through optional binding
            let id = jsonElement["id"] as? String
            let name = jsonElement["name"] as? String
            let pin = jsonElement["pin"] as? String
            let lastUpdated = jsonElement["created"] as? String
            let listAdminDeviceID = jsonElement["admin"] as? String

            tempContactsList.id = id
            tempContactsList.name = name
            tempContactsList.pin = pin
            tempContactsList.lastUpdated = lastUpdated
            tempContactsList.listAdmin = listAdminDeviceID

            downloadedLists.append(tempContactsList)

        }

        dispatch_async(dispatch_get_main_queue(), { () -> Void in

            self.delegate.listsDownloadingComplete(downloadedLists)

        })
    }
}

即使在iOS 9中,也不能保证
NSJSONSerialization.JSONObjectWithData(\uU4:options:)
是否返回可变对象。您应该指定了
NSJSONReadingOptions.MutableContainers

在代码中,您没有修改
jsonResult
,这意味着您无需将其声明为
NSMutableArray
。只需将
NSMutableArray
替换为
NSArray
,然后就不需要指定
NSJSONReadingOptions.MutableContainers

但正如瓦迪安所建议的,最好使用Swift类型,而不是
NSArray
NSDictionary
。这段代码应该可以在iOS 9和iOS 10中使用

func parseJSON() {

    var jsonResult: [[String: AnyObject]] = [] //<- use Swift type

    do{
        try jsonResult =  NSJSONSerialization.JSONObjectWithData(self.data, options: []) as! [[String: AnyObject]] //<- convert to Swift type, no need to specify options

    } catch let error as NSError {
        print(error)
    }

    var downloadedLists: [ContactsList] = []

    for jsonElement in jsonResult { //<- your for-in usage can be simplified

        let tempContactsList = ContactsList()

        //the following insures none of the JsonElement values are nil through optional binding
        let id = jsonElement["id"] as? String
        let name = jsonElement["name"] as? String
        let pin = jsonElement["pin"] as? String
        let lastUpdated = jsonElement["created"] as? String
        let listAdminDeviceID = jsonElement["admin"] as? String

        tempContactsList.id = id
        tempContactsList.name = name
        tempContactsList.pin = pin
        tempContactsList.lastUpdated = lastUpdated
        tempContactsList.listAdmin = listAdminDeviceID

        downloadedLists.append(tempContactsList)

    }

    dispatch_async(dispatch_get_main_queue(), { () -> Void in

        self.delegate.listsDownloadingComplete(downloadedLists)

    })
}
func parseJSON(){

var jsonResult:[[String:AnyObject]]=[]//使用Swift本机集合类型,而不是不相关的类型(可变)基金会类型,解决了您的问题。我有一个类似的问题,我的JSON解析器现在正在生成NSCORDEX而不是NSMutableDictionary。我需要继续使用可变字典,但是我认为解决方案应该是简单的,从某种意义上说,您可以创建一个可变的副本:MutabLealGeale=字典。.我想你可以为NSArray做一些类似的事情。一旦我有机会更详细地了解这件事,我会告诉你我的进展情况。。。