Mutating方法发送到不可变对象错误

时间:2013-03-19 09:16:48

标签: nsmutablearray nsmutabledictionary

Im receiving the next error:

由于未捕获的异常'NSInternalInconsistencyException'而终止应用程序,原因:' * - [NSCFDictionary setObject:forKey:]:发送到不可变对象的变异方法'

.h file
    @property(nonatomic, retain) NSMutableArray *arrayOfBenchmarks;

.m file
- (void)viewDidLoad
{
    [super viewDidLoad];

    arrayOfBenchmarks = [[NSMutableArray alloc]init];
    arrayOfBenchmarks = [self.currentDrill valueForKey:@"benchmarks"];

    for (int i=0; i<[arrayOfBenchmarks count]; i++) {

        UIButton *benchmarkButton = [UIButton buttonWithType:UIButtonTypeCustom];
        benchmarkButton.tag = i;
        benchmarkButton.frame = CGRectMake(40,20+(i*55) , 240, 50);
        [benchmarkButton setBackgroundImage:[UIImage imageNamed:@"red_color.png"] forState:UIControlStateNormal];
        benchmarkButton.titleLabel.textColor = [UIColor blackColor];
        benchmarkButton.titleLabel.textAlignment = NSTextAlignmentCenter;
        benchmarkButton.titleLabel.lineBreakMode = NSLineBreakByWordWrapping;
        NSString *tempString = [[[arrayOfBenchmarks objectAtIndex:i] valueForKey:@"benchmarkTitle"]  stringByAppendingString:@"\n"];
        [benchmarkButton setTitle:[tempString stringByAppendingString:@"00:00:00"] forState:UIControlStateNormal];
        [benchmarkButton addTarget:self action:@selector(onTapBenchmarkButton:)
             forControlEvents:UIControlEventTouchUpInside];
//        benchmarkButton.enabled = false;

        self.btn_scrollView.contentSize = CGSizeMake(0, 50+[arrayOfBenchmarks count]*50);
        [arrayOfButtons addObject:benchmarkButton];
        [self.btn_scrollView addSubview:benchmarkButton];
        dynamicBtnCounter++;
    }
}

- (void)onTapBenchmarkButton:(id)sender{

    int index = [sender tag];
    NSLog(@"index %i", index);
    NSString *tempString = [[[arrayOfBenchmarks objectAtIndex:index] valueForKey:@"benchmarkTitle"]  stringByAppendingString:@"\n"];
    [sender setTitle:[tempString stringByAppendingString:[timerLabel text]] forState:UIControlStateNormal];
    [sender setBackgroundColor:[UIColor colorWithPatternImage:[UIImage imageNamed:@"green_color.png"]]];
//    [sender setBackgroundImage:[UIImage imageNamed:@"green_color.png"]];
    NSMutableDictionary *benchmark = (NSMutableDictionary *)[arrayOfBenchmarks objectAtIndex:index];
    [benchmark setValue:[timerLabel text] forKey:@"benchmarkPerformedTimer"]; //giving exception at this line
    [sender setEnabled:NO];

}

这是我的Benchmark课程

@interface Benchmark : NSObject

@property (nonatomic, retain) NSString *bid;
@property (nonatomic, retain) NSString *benchmarkTitle;
@property (nonatomic, retain) NSString *benchmarkDescription;
@property (nonatomic, retain) NSString *benchmarkPerformedTimer;

-(id)initWithCoder: (NSCoder *)coder;
-(void)encodeWithCoder:(NSCoder *)encoder;

@end

1 个答案:

答案 0 :(得分:0)

我是通过添加可变副本完成的:

NSMutableDictionary *benchmark = (NSMutableDictionary *)[[arrayOfBenchmarks objectAtIndex:index] mutableCopy];
    [benchmark setValue:[timerLabel text] forKey:@"benchmarkPerformedTimer"];