1. Welcome to the new MacRumors forums. See our announcement and read our FAQ

ObjC: Problem with 2d array, NSPoints as NSValues

Discussion in 'Mac Programming' started by drs, Jun 4, 2009.

  1. drs
    macrumors newbie


    Using Objective C, I have a 2d array of NSValues holding NSPoints.
    At least, that's what I'm TRYING to do.

    Creation of 2d array, in init method:
    intersects = [[NSMutableArray arrayWithCapacity:19] retain];
    for (int i = 0; i < 19; i++) {
        NSMutableArray *row = [NSMutableArray arrayWithCapacity:19];
        [intersects addObject:row];
    I did try to use "intersects = [intersects initWithCapacity:19];" but that didn't work. It just didn't seem to register at all.

    Then in the method that finds the required points:
    I get the 'row' I need to work on: NSMutableArray *row = [intersects objectAtIndex:rNum];
    //Add point to 'row' array from 'intersects' - as NSPoint --> NSValue
    NSValue *pt = [NSValue valueWithPoint:NSMakePoint(w, h+y)];
    [row addObject:pt];
    /*Also tried [row addObject:[pt copy]; Didn't seem to work either.*/
    NSLog(@"(%i, %i)", w, h+y);
    NSLog(@"%@", pt);
    ^ The NSLogs print out the right thing. eg:
    (24, 24)
    NSPoint: {24, 24}
    Then I have a method which goes through the array and prints all the values - or at least, is supposed to:
    - (void) printPoints
        NSEnumerator *a = [intersects objectEnumerator];
        NSMutableArray *row;
        NSEnumerator *b;
        NSValue *v;
        while (row = [a nextObject]) {
             b = [row objectEnumerator];
             while (v = [b nextObject]) {
                     if (v = nil) {
                     }else {
                             NSLog(@"%@", v);
    Which gives me this:

    So, where am I going wrong?

    Please help me. This is the (fingers crossed) last stumbling block before I'm on the home stretch with this project.
  2. macrumors 68030


    if (v = nil) {
    should be

    if (v == nil) {
    or, my preferred form

    if(!v) { 
  3. macrumors 68040


    Disregard, I got confused.


Share This Page