gpt4 book ai didi

ios - swift 。 SIGABRT : cast value of type '__NSCFDictionary' to 'NSMutableArray'

转载 作者:行者123 更新时间:2023-11-28 06:54:24 25 4
gpt4 key购买 nike

我正在制作转换器应用程序。我想保存转换历史记录。我看到了this教程,它工作正常,但当我尝试在我的应用程序上使用它时,我收到了 SIGABRT。

Could not cast value of type '__NSCFDictionary' (0x57945c) to 'NSMutableArray' (0x5791c8)

我在

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

编辑:

应用委托(delegate):

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()
}
}

View Controller :

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

最佳答案

您正在尝试将 Dictionary 转换为 NSMutableArray

您可以通过将代码更改为将其保存为字典:

var notesDict: NSMutableDictionary = try NSPropertyListSerialization.propertyListWithData(data, options: NSPropertyListMutabilityOptions.MutableContainersAndLeaves, format: nil) as! NSMutableDictionary

关于ios - swift 。 SIGABRT : cast value of type '__NSCFDictionary' to 'NSMutableArray' ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34111323/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com