-1

I have an app based around a core data model and I am loading a data point back into a view controller. Here is the excpetion that I am getting:

 -[__NSCFString size]: unrecognized selector sent to instance 0xc530dd0

With exception breakpoints on, I have found that this line is causing the exception:

[self.MyImage setImage:[self.device valueForKey:@"imagename"]];

This is quite a weird error, as this line works:

[self.StepItTitle setText:[self.device valueForKey:@"title"]];

Why is it crashing? What do I need to fix to prevent this from happening?

C-O-D-E
  • 91
  • 1
  • 2
  • 8
  • What's your question? – Neeku Jul 08 '14 at 14:55
  • The exception says that you are calling `size` on a string object, which of course does not have that method. What is the result of `[self.device valueForKey:@"imagename"]` ? Try with `NSLog(@"%@", [self.device valueForKey:@"imagename"]);` – Francesco Jul 08 '14 at 15:02
  • @Neeku My question has been clarified above. – C-O-D-E Jul 08 '14 at 15:02
  • And I forgot: I imagine `MyImage` is an `NSImage` object? You should add some more information: at least type of objects, what is inside the `self.device` object. – Francesco Jul 08 '14 at 15:05
  • @Francesco When using NSLog before the crash, it shows the correct image name, exactly how I want. – C-O-D-E Jul 08 '14 at 15:05
  • @C-O-D-E the reason why I asked for `NSLog` output is that usually it prints the class name. For what you told me I expect that the object referenced by the key @"imagename" is a string then? – Francesco Jul 08 '14 at 15:07
  • When it crashes what do you see when you type this in the debugger? `po 0xc530dd0` or whatever the instance happens to be? – ansible Jul 08 '14 at 15:07
  • @Francesco Yep. The object is a string. I want my image to change to an image with the string from the core data model. – C-O-D-E Jul 08 '14 at 15:11
  • 1
    +1 for editing and clarifying. Always try to search well before asking, and once you asked, provide as much detail, including code and screenshots, as possible. (: Although: Questions seeking debugging help ("why isn't this code working?") must include the desired behavior, a specific problem or error and the shortest code necessary to reproduce it in the question itself. Questions without a clear problem statement are not useful to other readers. See: [How to create a Minimal, Complete, and Verifiable example](http://stackoverflow.com/help/mcve). – Neeku Jul 08 '14 at 15:12
  • @C-O-D-E And what is `self.MyImage`? an `UIImageView` ? Then you should do something `[self.MyImage setImage:[UIImage imageNamed:[self.device valueForKey:@"imagename"]]];` – Francesco Jul 08 '14 at 15:14
  • @Francesco It worked! Thanks a ton! Post an answer and I'll accept. – C-O-D-E Jul 08 '14 at 15:32
  • @C-O-D-E detailed answer added – Francesco Jul 08 '14 at 15:37
  • possible duplicate of [unrecognized selector sent to instance](http://stackoverflow.com/questions/2455161/unrecognized-selector-sent-to-instance) – Hot Licks Jul 08 '14 at 15:47
  • I suggest you read https://developer.apple.com/library/mac/documentation/Cocoa/Conceptual/CodingGuidelines/CodingGuidelines.html and follow it so that it's easier for others to read and understand your code. – duci9y Jul 08 '14 at 15:52

1 Answers1

1

Giving the comments to your question I add the following things:

self.MyImage is a reference to an object of type UIImageView and [self.device valueForKey:@"imagename"] returns an NSString with the image name.

Having said that, the exception clearly states that you (or the framework) is calling size to an NSString object. NSString does not have the method, thus the reason for the exception.

What you want to do instead, is to create a UIImage given a filename and set it to the view. To achieve this you have to do the following

NSString *filename = [self.device valueForKey:@"imagename"];
UIImage *image = [UIImage imageNamed:filename]; //This loads an image if present in the main bundle
[self.MyImage setImage:image];

Of course you can do the same in just one line of code, but I expended for clarity.

Francesco
  • 1,840
  • 19
  • 24