scojac
scojac

Reputation: 27

How can I correct this 'Incompatible Obj-c types' warning?

Specifically the warning is: "Incompatible Objective-C types 'struct NSString *', expected 'struct UIImage *' when passing argument 4 of 'objectWithType:name:code:image' from distinct Objective-C type". It follows a line that looks like so:

[Object objectWithType:@"Type" name:@"Name" code:@"0001" image:@"image.png"],

So, I understand that I created the class Object to take type UIImage, but I am providing it with type NSString. Here's the problem: I don't know how to indicate the image differently than its file name.

(Apologies if this is a basic problem. I'm new to this and trying to look for solutions before posting here. Any help you can offer is appreciated.)

Upvotes: 0

Views: 335

Answers (3)

Daniel
Daniel

Reputation: 664

If 'image.png' is included in your project bundle, then you can do this to pass a UIImage instead of an NSString:

[Object objectWithType:@"Type" name:@"Name" code:@"0001" image:[UIImage imageNamed:@"image.png"]];

Upvotes: 0

Jason Coco
Jason Coco

Reputation: 78363

You actually need an instance of UIImage, which you understand. So, the class method imageNamed: is typically used for this:

[Object objectWithType:@"Type" name:@"Name" code:@"0001" image:[UIImage imageNamed:@"image.png"]];

Another option (since you are using all strings here) might be to rewrite your method so that it takes a name instead of an image and then create the image inside the method implementation. So you might define the method:

- (void)objectWithType:(NSString*)type name:(NSString*)name code:(NSString*)code imageName:(NSString*)imageName
{
  UIImage* theImage = [UIImage imageNamed:imageName];
  // do whatever
}

Upvotes: 1

skorulis
skorulis

Reputation: 4381

Use [UIImage imageNamed:]. Will only work for images that are part of your project.

[Object objectWithType:@"Type" name:@"Name" code:@"0001" image:[UIImage imageNamed:@"image.png"]]

Upvotes: 0

Related Questions