0

allTiles is an NSMutuableArray declared with a capacity of 9. Next a loop inserts 9 NSNull objects with a method called resetArray. I have a method called addToArray which accepts an NSNumber and index at which to insert the value at.

In another class I have int currentTileValue = [self getTileValue]; which returns a tile value. This value will be both the number into the array as well as the index. Whenever I attempt to add to the array it crashes with no indication of why.

allTiles = [[NSMutableArray alloc] initWithCapacity:9];
[self resetArray:allTiles];

- (void) resetArray:(NSMutableArray*) array {
for (int i = 0; i < 9; i++) {
    [array insertObject:[NSNull null] atIndex:i];
}
}

- (void) addToArray:(NSNumber*)value :(int)index {
[allTiles insertObject: value atIndex:index];
}
//Different class
//Here is where I am trying to insert into the array.
[(BoxView *)self.superview addToArray:(NSNumber*)currentTileValue :currentTileValue];

What would be causing this to crash?

joshft91
  • 1,755
  • 7
  • 38
  • 53
  • 1
    Note that the crash info in a situation like this is certainly available, you just don't know how to find it. First off, add an exception breakpoint. (Select the breakpoints view, press the "+" at the bottom and select to add an exception breakpoint.) Next, see [this question](http://stackoverflow.com/questions/8100054/no-exception-stack-trace-in-console-under-xcode-4-2-ios-5). – Hot Licks Apr 13 '13 at 02:52
  • I am most definitely a new Objective-C programmer, thank you for the information/question link. – joshft91 Apr 13 '13 at 02:54

1 Answers1

2

You state that currentTitleValue is an int. Given this, you need to change this line:

[(BoxView *)self.superview addToArray:(NSNumber*)currentTileValue :currentTileValue];

to:

[(BoxView *)self.superview addToArray:@(currentTileValue) :currentTileValue];

You can't cast an int to an NSNumber pointer. You need to convert it.

Also, your addToArray method is incorrect. Since you pre-populate the array with 9 NSNull objects, you need to call replaceObjectAtIndex:withObject:, not insertObject:atIndex (this assumes you just want 9 values at all times):

[allTiles replaceObjectAtIndex:index withObject:value];

You should also rename your addToArray:: method to something better:

- (void)addToArray:(NSNumber*)value index:(int)index {
rmaddy
  • 314,917
  • 42
  • 532
  • 579
  • Great response, thank you very much. That also helped me with the expanding array question that I was going to ask next. When you say to rename it to something better, what would be a better name for it? – joshft91 Apr 13 '13 at 02:46
  • Okay, I see the difference now. What does that add to the method name? – joshft91 Apr 13 '13 at 02:48
  • Now you call it like this: `[(BoxView *)self.superview addToArray:@(currentTileValue) index:currentTileValue];`. – rmaddy Apr 13 '13 at 02:48