0

i'm quite new to objective C and followed some instructions i found, but at the moment i got stuck.

i am trying to load an image with the following code:

NSBitmapImageRep *img;
img = [NSBitmapImageRep imageRepWithContentsOfFile:filename];

"filename" is a valid pointer. But the compiler tells me:

Incompatible pointer types assigning to 'NSBitmapImageRep *' from 'NSImageRep * _Nullable' 

What am i missing or doing wrong ?

Michael Dautermann
  • 88,797
  • 17
  • 166
  • 215
Rolf
  • 85
  • 1
  • 6

1 Answers1

0

imageRepWithContentsOfFile: is a factory method. According documentation, it returns "An initialized instance of an NSImageRep subclass, or nil if the image data could not be read." From NSImageRep.h: "If sent to a subclass, does this just for the types understood by that subclass.". So, you need only cast result to specified type:

NSBitmapImageRep *imgRep = (NSBitmapImageRep *)[NSBitmapImageRep imageRepWithContentsOfFile:filename];

Note that usually you doesn't need to create NSBitmapImageRep instances yourself, and can use NSImage.

Borys Verebskyi
  • 4,160
  • 6
  • 28
  • 42
  • Thanks a lot, that solved my problem. As i had written before: i am very new to objective c and wasn't aware of this casting problem. – Rolf Feb 28 '16 at 11:09