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

    #1
    HELP!

    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:
    Code:
    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];
    Code:
    //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:
    Code:
    (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:
    Code:
    - (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) {
                             NSLog(@"Null");
                     }else {
                             NSLog(@"%@", v);
                     }
             }
        }
    }
    
    Which gives me this:
    (null)
    (null)
    (null)
    ...etc

    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

    Catfish_Man

    #2
    Code:
    if (v = nil) {
    should be

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

    Code:
    if(!v) { 
     
  3. macrumors 68040

    lee1210

    #3
    Disregard, I got confused.

    -Lee
     

Share This Page