【发布时间】:2013-01-16 19:31:17
【问题描述】:
我的问题是我有一个 NSMutableArray 类型的类属性,如我的头文件中所定义,但是当我尝试修改其中一个数组元素(NSDictionary)时,我收到以下运行时错误:
2013-01-16 14:17:20.993 debtaculous[5674:c07] * 终止应用程序到期 未捕获的异常“NSInternalInconsistencyException”,原因: '-[__NSCFArray replaceObjectAtIndex:withObject:]: 变异方法发送 到不可变对象'
标头声明:
// BudgetViewController.h
#import <UIKit/UIKit.h>
@interface BudgetViewController : UIViewController <UITableViewDataSource, UITableViewDelegate>
- (IBAction)afterTaxIncomeEditingDidEnd:(id)sender;
@property (strong, nonatomic) NSMutableArray *budgetArray;
@property (strong, nonatomic) IBOutlet UITextField *afterTaxIncome;
@property (strong, nonatomic) IBOutlet UITableView *budgetTableView;
@end
产生错误的方法:
-(void)applyCCCSWeights
{
NSMutableDictionary *valueDict;
NSString *newAmount;
for (id budgetElement in [self budgetArray]) {
valueDict = [[NSMutableDictionary alloc] initWithDictionary:budgetElement];
newAmount = [NSString stringWithFormat:@"%0.2f", [[self afterTaxIncome].text floatValue] * [[budgetElement objectForKey:@"cccs_weight"] floatValue]];
[valueDict setValue:newAmount forKeyPath:@"amount"];
[[self budgetArray] replaceObjectAtIndex:0 withObject:valueDict];
NSLog(@"%0.2f (%0.2f)", [[budgetElement objectForKey:@"amount"] floatValue], [[self afterTaxIncome].text floatValue] * [[budgetElement objectForKey:@"cccs_weight"] floatValue]);
}
[self.budgetTableView reloadData];
}
// 注意上面的replaceObjectAtIndex:0 只是一个占位符。这将替换为正确的索引。
【问题讨论】:
-
你在哪里初始化你的数组?
-
能否贴出初始化budgetArray的代码
-
主要部分:[self setUrlData:[NSURLConnection sendSynchronousRequest:request returnedResponse:&response error:&error]]; [self setBudgetArray:[NSJSONSerialization JSONObjectWithData:[self urlData] options:kNilOptions error:&error]];
-
你去...查看 NSJSONSerialization 中的选项。使用一种创建可变数组和字典的方法。
标签: ios objective-c xcode nsmutablearray