PDF not loading correctly


#1

I typed in the code for this chapter and double checked it, but the pdf that shows up does not print the first 2.5 employees. In other words, the PDF list starts at half way through the third employee in RaiseMan’s list. I’ve poured over this code and I can’t seem to find the error.

Code from my Xcodeproject (and the book):

- (void)drawRect:(NSRect)dirtyRect
{
    // The rectangle in which the name is drawn 
    // on this (off-page) printing view
    NSRect nameRect;
    
    // The rectangle in which the raise is drawn
    // on this (off-page) printing view
    NSRect raiseRect;
    
    // Set the size of the name- and raiseRects to 
    // AND the pageNumberRect
    // equal the height of the line
    raiseRect.size.height = nameRect.size.height = lineHeight;
    
    // Set the name's origin to start at the top of the page
    nameRect.origin.x = pageRect.origin.x;
    
    // Set the width of the name's frame
    nameRect.size.width = 200.0;
    
    // Set the raise's origin to start at the end of the name
    raiseRect.origin.x = NSMaxX(nameRect);
    
    // Set the width of the raise's frame
    raiseRect.size.width = 100.0;
    
    NSInteger i;
    // For every line, do: SEE_BELOW
    for (i = 0 ; i < linesPerPage ; i++) { 
        NSLog(@"Printing line %li", i);
        // THIS
        
        // Check to make sure you aren't looping more 
        // times than there are people!
        NSInteger index = (currentPage * linesPerPage) + i;
        if (index >= [people count]) {
            break;
        }
        RMPerson *p = [people objectAtIndex:index];
        
        // Draw index and name
        // Set the name to be drawn i lines down (y coordinate)
        nameRect.origin.y = pageRect.origin.y = (i * lineHeight);
        if ( i == 0 ) {
            NSLog(@"nameRect coordinates: (%f,%f)", nameRect.origin.x, nameRect.origin.y);
        }
        
        NSString *nameString = [NSString stringWithFormat:@"%2d %@", index + 1, [p personName]];
        NSLog(@"Printing person: %@", nameString);
        
        // Draw the name in nameRect
        [nameString drawInRect:nameRect withAttributes:attributes];
        
        // Draw the raise right below the last drawn name
        raiseRect.origin.y = nameRect.origin.y;
        NSString *raiseString = [NSString stringWithFormat:@"%4.2f%%", ([p expectedRaise] * 100.0)];
        
        // Draw the raise in raiseRect
        [raiseString drawInRect:raiseRect withAttributes:attributes];
    }
}

#2

Also, I just noticed I am getting these error logs in the console:

Jul  6 23:53:56 my-macbook.local RaiseMan[57287] <Error>: kCGErrorIllegalArgument: _CGSFindSharedWindow: WID -1
Jul  6 23:53:56 my-macbook.local RaiseMan[57287] <Error>: kCGErrorFailure: Set a breakpoint @ CGErrorBreakpoint() to catch errors as they are logged.
Jul  6 23:53:56 my-macbook.local RaiseMan[57287] <Error>: kCGErrorIllegalArgument: CGSSetWindowShadowAndRimParametersWithStretch: Invalid window 0xffffffff

#3

I figured it out (roughly). I added this line:

pageRect.origin.y -= lineHeight * 2.5;

in the method “knowsPageRange:” immediately below this line:

pageRect = [printInfo imageablePageBounds];

I have thoroughly enjoyed this book, but this really threw me off.