Pergunta

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.)

Foi útil?

Solução

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
}

Outras dicas

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"]]

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"]];
Licenciado em: CC-BY-SA com atribuição
Não afiliado a StackOverflow
scroll top