Combining PDF files in Cocoa (fast enumeration unimplemented)-Collection of common programming errors

I want to combine several PDFs into one by appending the pages of each to a single PDF. An answer to an identical question on this site gave the following code (assuming inputDocuments is an array of PDFDocuments:

PDFDocument *outputDocument = [[PDFDocument alloc] init];
NSUInteger pageIndex = 0;
for (PDFDocument *inputDocument in inputDocuments) {
    for (PDFPage *page in inputDocument) {
        [outputDocument insertPage:page atIndex:pageIndex++];
    }
}

Now, I don’t know if the PDFDocument class originally supported fast enumeration, but it doesn’t appear to now. I tried doing the same thing using a series of for-loops with an array of single-page PDFDocuments with the following:

    PDFDocument *outputDocument = [[PDFDocument alloc] init];
    NSUInteger aPageCount=0;

    for (PDFConverter* aConverter in [self finishedPDFConverters])
    {
        [outputDocument insertPage:[[aConverter theDoc] pageAtIndex:0] atIndex:aPageCount];
        aPageCount++;
    }

However I get the error

"2011-07-19 23:56:58.719 URLDownloader[37165:903] *** WebKit discarded an uncaught exception in the webView:didFinishLoadForFrame: delegate: *** -[NSCFArray insertObject:atIndex:]: index (1) beyond bounds (1)"

after the first Document has been added, so I end up with a PDF with only 1 page. How can I rectify this?

  1. Returning to this project after a long hiatus, I fixed the problem simply by looping through the PDFs from the last to the first, then looping through the pages backwards and inserting each at index 0. Convoluted, but it works….

  2. The error is not about fast enumeration.

    You tried to insert pages at an index that’s out of bounds (> than count). Try to replace insertPage:atIndex: by addPage:

Originally posted 2013-11-27 05:08:17.