Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

MrFusion

macrumors 6502a
Original poster
Jun 8, 2005
613
0
West-Europe
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
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
First, I am not sure why a copy is added to _searchResults, and why it is also not released.
Looks like a bug. I'd suggest reporting it.

Third, why is a PDFSelection returned, and not a NSNotification with PDFSelection as an object, as is "normally" done?
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.
 

MrFusion

macrumors 6502a
Original poster
Jun 8, 2005
613
0
West-Europe
Looks like a bug. I'd suggest reporting it.

I agree, and a bug report has been filed.

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.

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]);
}
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
If you're getting that selector error, then it means there's some memory problem going on elsewhere in your code.
 

MrFusion

macrumors 6502a
Original poster
Jun 8, 2005
613
0
West-Europe
If you're getting that selector error, then it means there's some memory problem going on elsewhere in your code.

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.
 

kainjow

Moderator emeritus
Jun 15, 2000
7,958
7
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.
 

MrFusion

macrumors 6502a
Original poster
Jun 8, 2005
613
0
West-Europe
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.

That make sense. Thanks for the clarification.
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.