0

In NSImage class reference it is said that there exist method initWithData, but why xCode says it doesn't exist?

I use it like this:

    NSData *imgData = [[imageView image] TIFFRepresentation];
fotoImg = [NSImage initWithData:imgData];

And i get warning:

Class method '+initWithData:' not found (return type defaults to 'id')

Why is this happening?

hockeyman
  • 1,141
  • 6
  • 27
  • 57

1 Answers1

4

You are calling initWithData on the class when you should call it on an instance of that class. Do this: [[NSImage alloc] initWithData:imgData];

It is very important to learn the difference between member functions and class functions (or in Obj C terms "messages"). I recommend http://www.amazon.de/Programming-Objective-C-Automatic-Reference-Developers/dp/0321811909/ref=pd_sim_sbs_eb_1/276-1733140-9413556.

Michel Müller
  • 5,535
  • 3
  • 31
  • 49
  • there is no "static way" in Objective-C. They are class methods and instance methods. The call is always dynamic. – newacct Jul 09 '12 at 19:48
  • Alright I corrected it. I didn't mean static as opposed to dynamic but you're right, it's not the correct term here. – Michel Müller Jul 09 '12 at 23:26
  • God, I was having this problem but with NSImage initWIthContentsOfFile and you really saved me a lot of time. I was converting code from iOS's [UIImage imageWithContentsOfFile] and just changed the class and method name assuming it would work. Brain fart moment, for sure. Hopefully if anyone has the same issue they wind up seeing this comment. – user3062913 Feb 10 '16 at 18:55