0

Right now I have a UIView with two subviews on it. I am trying to add another subview to one of those views, from a custom-made Card class. The problem is that the subview is coming from an NSMutableArray, and it makes the program crash every time I try to add it to any views. I know the issue has something to do with the NSMutableArray, because there isn't any problem if I just try to add an instance of Card that is initialized right then and there. What's the problem?

Here's the code:

Initialization of the main view where this is happening:

- (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil
{
    self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil];
    if (self) {

        mainMenu = [UIButton buttonWithType:UIButtonTypeRoundedRect];
        mainMenu.frame = CGRectMake(10, 944, 100, 50);
        [mainMenu setTitle:@"Main Menu" forState:UIControlStateNormal];
        [mainMenu addTarget:self action:@selector(goMainMenu) forControlEvents:UIControlEventTouchUpInside];

        reader = [[Reader alloc] init];
        deck = [reader getDeck];

        hand = [[Hand alloc] initWithFrame:CGRectMake(0, 1024/3, 768, 2048/3)];

        board = [[Board alloc] initWithFrame:CGRectMake(0, 0, 768, 1024/3)];
    }
    return self;
}

method where the card is supposed to be added:

- (void)viewDidLoad
{
    [super viewDidLoad];

    Card* card = [deck objectAtIndex:0];

    [hand addSubview:card];
    [self.view addSubview:hand];
    [self.view addSubview:board];
    [self.view addSubview:mainMenu];
}

and here is where the getDeck method is, in the Reader class:

-(NSMutableArray*)getDeck {

    NSMutableArray* deck = [NSMutableArray arrayWithCapacity:72];

    Card* card;

    NSMutableArray* strings = [[scanner string] componentsSeparatedByString:@"\n"];

    while ([strings count] > 0) {

        [strings removeObjectAtIndex:0];

        card = [Card cardWithExpressions:[strings subarrayWithRange:NSMakeRange(0, 4)]];
        card.frame = CGRectMake(0, 0, 180, 252);

        [card setCoefficient:[self getCoefficientFromExpression:[card getPolynomial]]];

        [deck addObject:card];

        [strings removeObjectsInRange:NSMakeRange(0, 4)];
    }

    return deck;
}
RaysonK
  • 553
  • 3
  • 9
  • 24
  • 2
    You're probably going to need to add more information about how your program is crashing. What error message do you get? Where do you get it? – Jesse Rusak Jun 17 '13 at 23:33
  • No error message. I'm using Xcode 4.2, which, as far as I know, doesn't give an error message. At least, it doesn't appear in the same place that it does in Xcode 3...Also, the error happens in the `main` method – RaysonK Jun 17 '13 at 23:35
  • it must show something in console area – Bryan Chen Jun 17 '13 at 23:36
  • 1
    Side note - if you are planning to write an app for the App Store, you need Xcode 4.5 or later. – rmaddy Jun 17 '13 at 23:37
  • 1
    @RaysonK Try following these instructions if you're seeing a crash which appears to be in main: http://stackoverflow.com/questions/7703052/xcode-doesnt-show-the-line-that-causes-a-crash – Jesse Rusak Jun 17 '13 at 23:37
  • @JesseRusak I'll give that a try and see if that helps. Also, I edited the part where the `card` object is initialized to reflect how the code looks with the exception gets thrown – RaysonK Jun 17 '13 at 23:42

1 Answers1

0

Solved. It turns out that the initializer that I was using for card, which was cardWithExpressions:, didn't have the [super initWithFrame:] in it. Put that in there and now it's all working.

RaysonK
  • 553
  • 3
  • 9
  • 24