gpt4 book ai didi

ios - 错误 : replaceObjectAtIndex:withObject:]: mutating method sent to immutable object

转载 作者:行者123 更新时间:2023-12-01 18:09:32 30 4
gpt4 key购买 nike

我的应用程序崩溃,但标题中提到了异常。请在下面找到我的解释:

尝试在我的 TableViewController 中使用 NSUserDefaults 存储数组值,如下所示:

func didDismissContentViewController(controller:ContentViewController,pageIndex:Int) { //You're passed in the contentViewController here, use relevant data to update your model. NSLog("Inside delegate method ..\n") var currentIdx = pageIndex

NSLog("Value of index edited \(pageIndex)")
NSLog("Current edited value is : \(controller.myTextView.text)")

**pageContent.replaceObjectAtIndex(pageIndex, withObject: controller.myTextView.text)**

**//pageCotent is defined as an NSMutableArray only...**

NSUserDefaults.standardUserDefaults().setObject(pageContent as NSMutableArray, forKey: "PageValues")
NSUserDefaults.standardUserDefaults().synchronize()
}
Retrieval is done as under : if var storedPageContent = NSUserDefaults.standardUserDefaults().objectForKey("PageValues") as? NSMutableArray{ pageContent = storedPageContent

虽然我能够在各自的页面中存储、检索和显示更新的页面值,但随后在 tableviewcontroller 和 content view controller 之间来回转换会引发以下错误:
015-12-21 00:51:46.592 PageApp[3943:203948] * Terminating app due to uncaught exception 'NSInternalInconsistencyException', reason: '-[__NSCFArray replaceObjectAtIndex:withObject:]: mutating method sent to immutable object' * First throw call stack:

我尝试将商店注释掉到 NSUserDefault,并且应用程序不会崩溃,而使用 NSUserDefault 存储值会导致这种情况发生,因此当我将其存储在 NSUserDefaults 中然后检索它并将其分配给数组时会出现错误。

对此的任何帮助将不胜感激。 Exchange data between uitah

最佳答案

NSUserDefaults 返回的数组是不可变的,因此您需要制作数组的可变副本,然后在 NSUserDefaults 中重新设置数组, 一旦你修改它。

我不太了解 Swift,无法给你一个代码示例,但这是在 Objective-C 中完成的:

NSUserDefaults *userDefaults = [NSUserDefaults standardUserDefauts];
NSMutableArray *pageContent = [[userDefaults objectForKey:@"PageValues"] mutableCopy];
[pageContent replaceObjectAtIndex:pageIndex withObject:controller.myTextView.text];
[userDefaults setObject:pageContent forKey:@"PageValues"];

关于ios - 错误 : replaceObjectAtIndex:withObject:]: mutating method sent to immutable object,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/34399498/

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