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.
我正在制作转换器应用程序。我想保存历史转换。我看到了这个教程,它工作正常,但当我尝试在我的应用程序上使用它时,我得到一个SIGABRT。
Could not cast value of type '__NSCFDictionary' (0x57945c) to 'NSMutableArray' (0x5791c8)
无法将类型'__NSCFDictionary'(0x57945c)的值转换为'NSMutableArray'(0x5791c8)
I'm getting this at
我得到了这个
notesArray = try NSPropertyListSerialization.propertyListWithData(data, options: NSPropertyListMutabilityOptions.MutableContainersAndLeaves, format: nil) as! NSMutableArray
notesArray =尝试NSPropertyListSerialization.propertyListWithData(数据,选项:NSPropertyListMutabilityOptions.MutableContainersAndLeaves,格式:nil)为! NSMutableArray里
EDIT:
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)
}
}
2 个解决方案
#1
1
You are trying to cast a Dictionary
into a NSMutableArray
.
您正在尝试将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
#2
0
Believe I had this same problem, and from your code it looks like we also copied from the same example. I prepared my plist file and XCode did let my call the root an array, but my data really wasn't in an array format, it was a dictionary, and that is the format in which it was saved.
相信我有同样的问题,从你的代码看起来我们也复制了相同的例子。我准备了我的plist文件,而XCode确实让我把root调用为一个数组,但是我的数据确实不是数组格式,而是字典,这就是保存它的格式。
When I reloaded the plist file, NSPropertyListSerialization was smart enough to see my data was really a dictionary, and so the cast to NSMutableArray failed (BOOM!). Sure enough, when I opened the saved plist file and examined it, the XML clearly showed it had been saved as a dictionary. So, think about the data you are saving and if it really is a dictionary, change the cast to NSMutableDictionary and unpack your data using that format.
当我重新加载plist文件时,NSPropertyListSerialization非常聪明,可以看到我的数据真的是一个字典,因此对NSMutableArray的强制转换失败了(BOOM!)。果然,当我打开保存的plist文件并检查它时,XML清楚地显示它已被保存为字典。因此,考虑一下您要保存的数据,如果它确实是一个字典,请将转换更改为NSMutableDictionary并使用该格式解压缩数据。
#1
1
You are trying to cast a Dictionary
into a NSMutableArray
.
您正在尝试将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
#2
0
Believe I had this same problem, and from your code it looks like we also copied from the same example. I prepared my plist file and XCode did let my call the root an array, but my data really wasn't in an array format, it was a dictionary, and that is the format in which it was saved.
相信我有同样的问题,从你的代码看起来我们也复制了相同的例子。我准备了我的plist文件,而XCode确实让我把root调用为一个数组,但是我的数据确实不是数组格式,而是字典,这就是保存它的格式。
When I reloaded the plist file, NSPropertyListSerialization was smart enough to see my data was really a dictionary, and so the cast to NSMutableArray failed (BOOM!). Sure enough, when I opened the saved plist file and examined it, the XML clearly showed it had been saved as a dictionary. So, think about the data you are saving and if it really is a dictionary, change the cast to NSMutableDictionary and unpack your data using that format.
当我重新加载plist文件时,NSPropertyListSerialization非常聪明,可以看到我的数据真的是一个字典,因此对NSMutableArray的强制转换失败了(BOOM!)。果然,当我打开保存的plist文件并检查它时,XML清楚地显示它已被保存为字典。因此,考虑一下您要保存的数据,如果它确实是一个字典,请将转换更改为NSMutableDictionary并使用该格式解压缩数据。