我正在制作一个转换器应用程序。我想保存转换历史。我看过this教程,它工作正常,但当我试图在我的应用程序上使用它时,我得到了一个SIGABRT。
无法将类型“__NSCFDictionary”(0x57945c)的值强制转换为“NSMutableArray”(0x5791c8)
我在...notesArray = try NSPropertyListSerialization.propertyListWithData(data, options: NSPropertyListMutabilityOptions.MutableContainersAndLeaves, format: nil) as! NSMutableArray
编辑:
应用程序委派:
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()
}
}
视图控制器:
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条答案
按热度按时间anauzrmj1#
您正在尝试将
Dictionary
转换为NSMutableArray
。您可以将程式码变更为下列项目,将它储存为字典:
ndh0cuux2#
相信我也遇到过同样的问题,从你的代码看,我们也是从同一个例子复制来的。我准备了我的plist文件,XCode确实让我把根称为数组,但我的数据实际上不是数组格式,而是字典,这就是它被保存的格式。
当我重新加载plist文件时,NSPropertyListSerialization非常聪明,能够看到我的数据实际上是一个字典,因此向NSMutableArray的转换失败(BOOM!)。果然,当我打开保存的plist文件并检查它时,XML清楚地显示它已经被保存为字典。所以,想想你正在保存的数据,如果它真的是字典,请将转换更改为NSMutableDictionary并使用该格式解包数据。