Page 1 of 1

Why Doesn't The Attached Program Compile?

PostPosted: Thu Aug 26, 2010 8:39 am
by dekiefer
Hi,

I'm having trouble getting OB to accept the NSImage function TIFFRepresentation. I'm sure I'm missing something in the definition or the declaration but, for the life of me, I can't figure it out. An example of the problem is attached.

I'll appreciate any suggestions,

Duane

Re: Why Doesn't The Attached Program Compile?

PostPosted: Thu Aug 26, 2010 11:04 am
by berndnoetscher
You have an error in your code.

Code: Select all

 Dim mydata2myTiff As NSImage = myData.TIFFRepresentation()


Cannot work, because myData is of type NSData and TIFFRepresentation has been correctly declared for a NSImage.

If you want to set your NSImage from NSData use the code below.

Code: Select all

'above:
Declare Function "NSImage" + (id) alloc

'in init:

Dim mydata2myTiff As NSImage = NSImage.alloc()

mydata2myTiff.initWithData(myData)

Re: Why Doesn't The Attached Program Compile?

PostPosted: Thu Aug 26, 2010 11:13 pm
by dekiefer
Bernd,

Thanks for the coding pointers. I like your method of teaching.

Here's how I got your suggestion to work:

----------------------------->
'above added:
Declare Function "NSImage" - (id)initWithData:(NSData *)data

'in init changed your suggestion:
'mydata2myTiff.initWithData(myData)
'to:
mydata2myTiff = NSImage.initWithData(myData)

'and then this works
mydata2myTiff.TIFFRepresentation()

<-------------------------------------------------

Thank you for taking the time to help me.

Duane

Re: Why Doesn't The Attached Program Compile?

PostPosted: Fri Aug 27, 2010 7:06 am
by dekiefer
Bernd,

The TestTIFF program compiles but when it's Run it throws the Exception: NSInvalidArgument Exception: +[NSImage initWithData:]: unrecognized selector sent to class.

I should have run it before I responded.

I've attached the Project when generates the exception.

I'd appreciate any guidance you might offer.

Duane

Re: Why Doesn't The Attached Program Compile?

PostPosted: Fri Aug 27, 2010 7:31 am
by berndnoetscher
dekiefer wrote:Bernd,

The TestTIFF program compiles but when it's Run it throws the Exception: NSInvalidArgument Exception: +[NSImage initWithData:]: unrecognized selector sent to class.

I should have run it before I responded.

I've attached the Project when generates the exception.

I'd appreciate any guidance you might offer.

Duane


What happens with my code? Your new code cannot work, because you mix class and instance methods.

Re: Why Doesn't The Attached Program Compile?

PostPosted: Fri Aug 27, 2010 6:13 pm
by dekiefer
berndnoetscher wrote:
dekiefer wrote:Bernd,

The TestTIFF program compiles but when it's Run it throws the Exception: NSInvalidArgument Exception: +[NSImage initWithData:]: unrecognized selector sent to class.

I should have run it before I responded.

I've attached the Project which generates the exception.

I'd appreciate any guidance you might offer.

Duane


What happens with my code? Your new code cannot work, because you mix class and instance methods.


The changes you suggested wouldn't compile. As I pointed out in my response:
'in init changed your suggestion:
'mydata2myTiff.initWithData(myData) <-- Your suggestion wouldn't compile
'to:
mydata2myTiff = NSImage.initWithData(myData) <-- So I changed it to this

Duane

Re: Why Doesn't The Attached Program Compile?

PostPosted: Sat Aug 28, 2010 8:58 pm
by dekiefer
Your new code cannot work, because you mix class and instance methods.


Please explain what you mean. Here's what the developer documentation says about 'NSImage initWithData':
---------------------------------------->>>
initWithData:
Initializes and returns an NSImage instance with the contents of the specified NSData object.

- (id)initWithData:(NSData *)data

Parameters
data -- The data object containing the image data.

Return Value -- An initialized NSImage instance, or nil if the method cannot create an image representation from the contents of the specified data object.

Availability -- Available in Mac OS X v10.0 and later.

Declared In NSImage.h
<<<--------------------
Does this not indicate that NSImage is to be initialized with NSData?

Re: Why Doesn't The Attached Program Compile?

PostPosted: Mon Aug 30, 2010 10:31 am
by berndnoetscher
<<Does this not indicate that NSImage is to be initialized with NSData?

Yes.

Again watch out your declare statements
a + declares a class method
a - declares an instance method of an object

you mix it in your code.
Code: Select all
Declare Class "NSURL"
Declare Function "NSURL" + (id)URLWithString:(NSString *)URLString

Declare Function "NSData" + (id)dataWithContentsOfURL:(NSURL *)aURL

Declare Function "NSImage" - (id)initWithData:(NSData *)Data

Declare Function "NSImage" + (id) alloc


Event AwakeFromNib()
  Dim myData As NSData
  Dim myURL As NSURL
  Dim gifURL As String

  gifURL = "http://www.gifs.net/Animation11/Animals/Bears_and_Pandas/American_bear.gif" 
  myURL = NSURL.URLWithString(gifURL)

  myData = NSData.dataWithContentsOfURL(myURL)

Dim mydata2myTiff As NSImage = NSImage.alloc()

mydata2myTiff.initWithData(myData)
 


End Event

Re: Why Doesn't The Attached Program Compile?

PostPosted: Mon Aug 30, 2010 9:55 pm
by dekiefer
Thank you Bernd!

I appreciate your patience,

Duane