迅速. SIGABRT:将类型'__NSCFDictionary'的值强制转换为'NSMutableArray' [英] Swift. SIGABRT: cast value of type '__NSCFDictionary' to 'NSMutableArray'
问题描述
我正在制作一个转换器应用程序.我想保存转换历史记录. 我看到了此教程,效果很好,但是当我尝试在自己的应用中使用它时,我得到了SIGABRT.
I'm making a converter app. I want to save the conversion historic. I saw this tutorial and it works fine but when I tried to use it on my app I'm getting a SIGABRT.
无法将类型'__NSCFDictionary'(0x57945c)的值强制转换为'NSMutableArray'(0x5791c8)
Could not cast value of type '__NSCFDictionary' (0x57945c) to 'NSMutableArray' (0x5791c8)
我在
notesArray = try NSPropertyListSerialization.propertyListWithData(data, options: NSPropertyListMutabilityOptions.MutableContainersAndLeaves, format: nil) as! NSMutableArray
AppDelegate:
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:
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)
}
}
推荐答案
您正在尝试将Dictionary
转换为NSMutableArray
.
您可以通过将代码更改为以下内容将其另存为字典:
You could save it as a Dictionary by changing your code to:
var notesDict: NSMutableDictionary = try NSPropertyListSerialization.propertyListWithData(data, options: NSPropertyListMutabilityOptions.MutableContainersAndLeaves, format: nil) as! NSMutableDictionary
这篇关于迅速. SIGABRT:将类型'__NSCFDictionary'的值强制转换为'NSMutableArray'的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!