PDFselection

Discussion in 'Mac Programming' started by MrFusion, Feb 19, 2009.

  1. macrumors 6502a

    Joined:
    Jun 8, 2005
    Location:
    West-Europe
    #1
    I am trying to understand pdfkit, and the apple example PDFKitViewer.

    I have a pdf that I want to search (which is working in the apple example), but of course not in my version.
    Most confusing is this snippet.

    Code:
    - (void) didMatchString:(PDFSelection *) instance{
    	// Add page label to our array.
    	PDFSelection *searchHit = (PDFSelection *)[instance copy];
    	[pdfSearchResults addObject:searchHit];
    	[searchHit release];
    	
    	// Force a reload.
    	[pdfSearchResultsView reloadData];
    	
    	NSLog(@"\n%@\n",[[[[instance copy] pages] objectAtIndex: 0] label]); \\does not work
    }
    
    adapted from
    Code:
    - (void) didMatchString: (PDFSelection *) instance{
    	// Add page label to our array.
    	[_searchResults addObject: [instance copy]];
    
    	// Force a reload.
    	[_searchTable reloadData];
    	
    	NSLog(@"\n%@\n",[[[[instance copy] pages] objectAtIndex: 0] label]); \\ works fine
    }
    
    First, I am not sure why a copy is added to _searchResults, and why it is also not released.
    Second, when I try to use the "instance" in my program, I get "-[NSConcreteNotification pages]: unrecognized selector sent to instance". The NSLog line does work in the apple example. The apple example is a single class, so what am I overlooking?
    Third, why is a PDFSelection returned, and not a NSNotification with PDFSelection as an object, as is "normally" done?

    Thanks
     
  2. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #2
    Looks like a bug. I'd suggest reporting it.

    It's a delegate method, not a notification. Notifications are generic and sent out to anything, but delegate methods are called only on a single delegate object.
     
  3. thread starter macrumors 6502a

    Joined:
    Jun 8, 2005
    Location:
    West-Europe
    #3
    I agree, and a bug report has been filed.

    Ok.

    I still find it strange that the exact same line works in their delegate but not in my mine.
    I have reduced the function to a single line, which works in the apple example, but not in mine.

    Code:
    - (void) didMatchString:(PDFSelection *) instance{
    	NSLog(@"\n_L%@_\n\n",[[[instance pages] objectAtIndex: 0] label]);
    }
    
     
  4. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #4
    If you're getting that selector error, then it means there's some memory problem going on elsewhere in your code.
     
  5. thread starter macrumors 6502a

    Joined:
    Jun 8, 2005
    Location:
    West-Europe
    #5
    I am not sure this is related to memory or not. I leave that question to the experts. But I did found out where the problem was.

    If I set the delegate of the pdfdocument, then there is no problem.
    [_pdf setDelegate:self];

    What I did was this. I registered an observer for the 4 notifications required do to finding. I know that if you set the delegate, the delegate is automatically registered for delegate notifications. But setting the notifications yourself should be equally fine, or not?

    Code:
    	[[NSNotificationCenter defaultCenter] addObserver: self 
    											 selector: @selector(startFind:) 
    												 name: PDFDocumentDidBeginFindNotification 
    											   object: _pdf];
    	
    	[[NSNotificationCenter defaultCenter] addObserver: self
    											 selector: @selector(findProgress:) 
    												 name: PDFDocumentDidEndPageFindNotification
    											   object: _pdf];
    	
    	[[NSNotificationCenter defaultCenter] addObserver: self 
    											 selector: @selector(endFind:) 
    												 name: PDFDocumentDidEndFindNotification
    											   object: _pdf];
    	
    	[[NSNotificationCenter defaultCenter] addObserver: self 
    											 selector: @selector(didMatchString:) 
    												 name: PDFDocumentDidFindMatchNotification
    											   object: _pdf];
    
    
    -------
    edit

    aaarghhh....
    PDFKitViewer is a ...(insert many foul words)... example. The example registers for the 3 of the 4 notifications with its own functions, instead of using the delegate methods. The fourth notification, the only one that is actually required to do any searching, is used through the delegate method. This of course doesn't work if you don't set the delegate. The example of course did not register for this notification, because it did set the delegate.
    The example might be correct as in it is working, but it's internally inconsistent and buggy. It's also utterly frustrating if you are new to PDFKit. It doesn't help either, that google doesn't turn up many hits on PDFKit.

    So, if you want to do anything (basic) with pdf's:
    Import the quartz framework and read the documentation for the following classes PDFDocument, PDFThumbnailView and PDFSelection.
     
  6. Moderator emeritus

    kainjow

    Joined:
    Jun 15, 2000
    #6
    That is why you were having the error. You're mixing up notifications and delegate methods. You implemented the delegate method, but were registering for a notification with that same selector. Notifications send an NSNotification object, but you were implementing it as a PDF* object and that is why it was giving you the error with the object not responding to the pages selector.

    And I should have said "probably" is a memory error, because usually when you have a memory error you get selectors sent to the wrong object, but in your case you're sending a selector to an NSNotification object.
     
  7. thread starter macrumors 6502a

    Joined:
    Jun 8, 2005
    Location:
    West-Europe
    #7
    That make sense. Thanks for the clarification.
     

Share This Page