迅速。 SIGABRT:类型' __ NSCFDictionary'的转换值到' NSMutableArray'

时间:2015-12-05 21:49:22

标签: ios swift swift2

我正在制作转换器应用。我想保存历史转换。 我看到了this教程,它运行正常,但当我尝试在我的应用程序上使用它时,我得到了一个SIGABRT。

  

无法转换类型' __ NSCFDictionary'的值(0x57945c)到' NSMutableArray' (0x5791c8)

我在

得到这个

notesArray = try NSPropertyListSerialization.propertyListWithData(data, options: NSPropertyListMutabilityOptions.MutableContainersAndLeaves, format: nil) as! NSMutableArray

编辑:

的AppDelegate:

import UIKit

@UIApplicationMain
class AppDelegate: UIResponder, UIApplicationDelegate {

    var window: UIWindow?

    var plistPathInDocument:String = String()

    func preparePlistForUse(){

        let rootPath = NSSearchPathForDirectoriesInDomains(NSSearchPathDirectory.DocumentDirectory, .UserDomainMask, true)[0]

        plistPathInDocument = rootPath.stringByAppendingString("/historic.plist")
        if !NSFileManager.defaultManager().fileExistsAtPath(plistPathInDocument){
            let plistPathInBundle = NSBundle.mainBundle().pathForResource("historic", ofType: "plist") as String!

            do {
                try NSFileManager.defaultManager().copyItemAtPath(plistPathInBundle, toPath: plistPathInDocument)
            }catch{
                print("Error occurred while copying file to document \(error)")
            }
        }
    }

    func application(application: UIApplication, didFinishLaunchingWithOptions launchOptions: [NSObject: AnyObject]?) -> Bool {
        // Override point for customization after application launch.
        self.preparePlistForUse()
        return true
    }

    func applicationDidBecomeActive(application: UIApplication) {
        // Restart any tasks that were paused (or not yet started) while the application was inactive. If the application was previously in the background, optionally refresh the user interface.
        self.preparePlistForUse()
    }
}

的ViewController:

import UIKit

class ViewControllerHistorico: UITableViewController {

    var notesArray:NSMutableArray!
    var plistPath:String!

    override func viewWillAppear(animated: Bool) {
        let appDelegate = UIApplication.sharedApplication().delegate as! AppDelegate
        plistPath = appDelegate.plistPathInDocument
        // Extract the content of the file as NSData
        let data:NSData =  NSFileManager.defaultManager().contentsAtPath(plistPath)!
        do{
            notesArray = try NSPropertyListSerialization.propertyListWithData(data, options: NSPropertyListMutabilityOptions.MutableContainersAndLeaves, format: nil) as! NSMutableArray
        }catch{
            print("Error occured while reading from the plist file")
        }
        self.tableView.reloadData()
    }

    override func tableView(tableView: UITableView,
        cellForRowAtIndexPath indexPath: NSIndexPath) -> UITableViewCell{

            let cell:UITableViewCell! = tableView.dequeueReusableCellWithIdentifier("cellIdentifier")
            cell.textLabel!.text = notesArray.objectAtIndex(indexPath.row) as? String
            return cell
    }
    override func tableView(tableView: UITableView,
        numberOfRowsInSection section: Int) -> Int{
            return notesArray.count
    }

    override func tableView(tableView: UITableView,
        commitEditingStyle editingStyle: UITableViewCellEditingStyle,
        forRowAtIndexPath indexPath: NSIndexPath){
            // remove the row from the array
            notesArray.removeObjectAtIndex(indexPath.row)
            self.tableView.deleteRowsAtIndexPaths([indexPath], withRowAnimation: UITableViewRowAnimation.Fade)
            notesArray.writeToFile(plistPath, atomically: true)
    }
}

historic.plist

2 个答案:

答案 0 :(得分:1)

您正在尝试将Dictionary投射到NSMutableArray

您可以将代码更改为:

,将其另存为字典
var notesDict: NSMutableDictionary = try NSPropertyListSerialization.propertyListWithData(data, options: NSPropertyListMutabilityOptions.MutableContainersAndLeaves, format: nil) as! NSMutableDictionary

答案 1 :(得分:0)

相信我有同样的问题,从你的代码看起来我们也从同一个例子中复制了。我准备了我的plist文件,而XCode确实让我的root调用了一个数组,但我的数据确实不是数组格式,而是字典,这就是保存它的格式。

当我重新加载plist文件时,NSPropertyListSerialization非常聪明,可以看到我的数据真的是一个字典,因此对NSMutableArray的强制转换失败了(BOOM!)。果然,当我打开保存的plist文件并检查它时,XML清楚地表明它已被保存为字典。因此,考虑一下您要保存的数据,如果它确实是字典,请将演员表更改为NSMutableDictionary并使用该格式解压缩数据。