Home | Contact Us | FAQ | Search & Site Map | Link to Us
Sign In | Join | Other 45 Sites in Network
Home
Discussion Groups
General
GeneralPortable MacsHardwareNetworking
Applications
Mac ApplicationsEudoraFirefox / MozillaInternet ExplorerOutlook ExpressMS OfficeEntourageExcelPowerPointWordVirtual PCMedia PlayerOther MS Products
Programming
Mac ProgrammingCodeWarriorPerl
Country Specific
Australian Mac GroupUK Mac Group

Mac Forum / Programming / Mac Programming / June 2005



Tip: Looking for answers? Try searching our database.

How to describe external Object?

Thread view: 
Enable EMail Alerts  Start New Thread
Thread rating: 
zamiran - 28 Jun 2005 15:39 GMT
I'm trying to describe an object which should be visible to every
other in the module. I'm trying:

in .h file :

extern PDFDocument *temp1PDF;

and in .m file :
PDFDocument *temp1PDF = [[[PDFDocument alloc]
initWithURL: [NSURL: [NSURL fileURLwithPath: @"/volumes..../"]]
autorelease];

the compiler says : error: initializer element is not constant.
How can I solve this problem, I can declare NSString , but this
PDFDocument fails. Please Help, Thanks.

* posted via http://www.mymac.ws
* please report abuse to http://xinbox.com/mymac
Tom Harrington - 28 Jun 2005 16:23 GMT
> I'm trying to describe an object which should be visible to every
> other in the module. I'm trying:
[quoted text clipped - 11 lines]
> How can I solve this problem, I can declare NSString , but this
> PDFDocument fails. Please Help, Thanks.

So this line is not within any method of function, but is the global
declaration in the file?  That's why.  You can initialize an NSString
outside of a block of code like this:

NSString *foo = @"bar";

But that only works because @"bar" is a constant string, so the compiler
can deal with it.  You're including allocation and initialization,
meaning that what you're assigning to temp1PDF is not a constant.

You need to trim that down to just the declaration:

PDFDocument *temp1PDF;

...and then put the alloc/init inside a block of code somewhere,
presumably an initialization method.

Signature

Tom "Tom" Harrington
Macaroni, Automated System Maintenance for Mac OS X.
Version 2.0:  Delocalize, Repair Permissions, lots more.
See http://www.atomicbird.com/

zamiran - 28 Jun 2005 17:14 GMT
> You need to trim that down to just the declaration:
> PDFDocument *temp1PDF;
> ....and then put the alloc/init inside a block of code somewhere,
> presumably an initialization method.

I tried to do like you said :

#import "AppController.h"

PDFDocument *temp1PDF;

@implementation AppController

- (id) init
{

temp1PDF= [[[PDFDocument alloc] initWithURL: [NSURL
fileURLWithPath:path_file]] autorelease];
return self;
}
- (IBAction)LoadPDF:(id)sender
{
NSString *filename = @"/volumes/macintosh hd/PDFS/test.pdf";
docPDF = [[[PDFDocument alloc] initWithURL: [NSURL
fileURLWithPath:filename]] autorelease];
}

- (IBAction)writePdf:(id)sender
{

NSString *search = @"3533168";
[docPDF setDelegate :self];
[docPDF beginFindString: search withOptions: NSCaseInsensitiveSearch];

}

- (void) didMatchString: (PDFSelection *) instance
{

[temp1PDF insertPage: [docPDF pageAtIndex:0]   atIndex: 0];
++pp ;

}

It has no errors during the compilation, but it raises runtime error
when the delegate is called and tries to write something to
temp1PDF.
What should it be ? Thanks a lot!!

* posted via http://www.mymac.ws
* please report abuse to http://xinbox.com/mymac
Tom Harrington - 28 Jun 2005 17:43 GMT
> It has no errors during the compilation, but it raises runtime error
> when the delegate is called and tries to write something to
> temp1PDF.
> What should it be ? Thanks a lot!!

I have no idea.  Saying it has run-time errors is an extremely vague
description of the problem.  Maybe if you read what those error messages
actually say, you'll get some idea of what's causing them.

Signature

Tom "Tom" Harrington
Macaroni, Automated System Maintenance for Mac OS X.
Version 2.0:  Delocalize, Repair Permissions, lots more.
See http://www.atomicbird.com/

Michael Ash - 28 Jun 2005 17:51 GMT
>> You need to trim that down to just the declaration:
>> PDFDocument *temp1PDF;
[quoted text clipped - 14 lines]
> temp1PDF= [[[PDFDocument alloc] initWithURL: [NSURL
> fileURLWithPath:path_file]] autorelease];

By putting an autorelease here, you're saying, "I'm not interested in this
object past the end of the current method", which is not true. So the
system is destroying the object and leaving you with a stale reference in
temp1PDF, which then explodes when you touch it.

Proper memory management is vital for creating Cocoa apps. This web page
is a great starting point for learning about the basics:

http://www.cocoadev.com/index.pl?MemoryManagement

I particularly recommend this page:

http://www.stepwise.com/Articles/Technical/HoldMe.html

On another note, the +initialize method is usually a better place for
these one-time initializations.
zamiran - 28 Jun 2005 18:49 GMT
> By putting an autorelease here, you're saying, "I'm not interested
> in this
> object past the end of the current method", which is not true. So the
> system is destroying the object and leaving you with a stale
> reference in
> temp1PDF, which then explodes when you touch it.

> Proper memory management is vital for creating Cocoa apps. This web
> page
> is a great starting point for learning about the basics:

Thank you very much, It works now. I'll read everything. Thanks again.

* posted via http://www.mymac.ws
* please report abuse to http://xinbox.com/mymac
 
Sign In
Join
My Latest Posts
My Monitored Threads
My Blog
My Photo Gallery
My Profile
My Homepage

Start New Thread
Enable EMail Alerts
Rate this Thread



©2008 Advenet LLC   Privacy Policy - Terms of Use
This website includes both content owned or controlled by Advenet as well as content owned or controlled by third parties.