[英]Swift. SIGABRT: cast value of type '__NSCFDictionary' to 'NSMutableArray'
我正在制作一個轉換器應用程序。 我想保存轉換歷史記錄。 我看到了本教程,並且效果很好,但是當我嘗試在自己的應用程序上使用它時,我得到了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()
}
}
視圖控制器:
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
。
您可以通過將代碼更改為以下內容將其另存為字典:
var notesDict: NSMutableDictionary = try NSPropertyListSerialization.propertyListWithData(data, options: NSPropertyListMutabilityOptions.MutableContainersAndLeaves, format: nil) as! NSMutableDictionary
相信我也遇到了同樣的問題,從您的代碼看來,我們也從同一示例中復制了該代碼。 我准備了plist文件,XCode確實允許我將根稱為數組,但是我的數據實際上不是數組格式,而是字典,這就是保存數據的格式。
當我重新加載plist文件時,NSPropertyListSerialization非常聰明,可以看到我的數據確實是一個字典,因此對NSMutableArray的強制轉換失敗(BOOM!)。 果然,當我打開保存的plist文件並進行檢查時,XML清楚地表明它已被保存為字典。 因此,請考慮要保存的數據,如果它確實是字典,則將演員表更改為NSMutableDictionary,然后使用該格式解壓縮數據。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.