Welcome to the Treehouse Community

Want to collaborate on code errors? Have bugs you need feedback on? Looking for an extra set of eyes on your latest project? Get support with fellow developers, designers, and programmers of all backgrounds and skill levels here with the Treehouse Community! While you're at it, check out some resources Treehouse students have shared here.

Looking to learn something new?

Treehouse offers a seven day free trial for new students. Get access to thousands of hours of content and join thousands of Treehouse students and alumni in the community today.

Start your free trial

iOS

Stuck in code

Hello, i'm doing the iOS course, and i'm in the Ribbit app, Build a Self-Destructing Message iPhone App, on the last section.

I have some troubles to run the app, but i cant figure out what is the problem with my code.

This is the part that throw an error. In the first line on PFFile.

- (void)viewDidLoad

{

[super viewDidLoad];

PFFile *imageFile = [self.message objectForKey:@"file"];

NSURL *imageFileURL = [[NSURL alloc]initWithString:imageFile.url];

NSData *imageData = [NSData dataWithContentsOfURL:imageFileURL];

self.imageView.image = [UIImage imageWithData:imageData];

NSString *senderName = [self.message objectForKey:@"senderName"];

NSString *title = [NSString stringWithFormat:@"Sent by %@", senderName];

self.navigationItem.title = title; 

}

Please help me. I'm really stuck on this.

24 Answers

What error throws?

Maybe an error related for key without data.

objectForKey looks if the object exists.

The error is: Ribbit[465:60b] -[UIImageView objectForKey:]: unrecognized selector sent to instance 0x9d54d70.

And this happen when i try to view a image from my inbox.

I have in my Dashboard on Parse.com a object called "file" that hold the images or videos.

You need to fetch data.

From Parse Docs:

PFFile *userImageFile = anotherPhoto[@"imageFile"];
[userImageFile getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {
    if (!error) {
        UIImage *image = [UIImage imageWithData:imageData];
    }
}];

So for this example will be

PFFile *imageFile = [self.message objectForKey:@"file"];

[imageFile getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {
    if (!error) {
        self.imageView.image = [UIImage imageWithData:imageData];
    }
}];

i used this, but the same error. What i have to modified? Really don't get it. "message" is a PFObject that contains the saved information in Parse, and the column with the images called "file".

        <p> PFFile *imageFile = [self.message objectForKey:@"file"];
[imageFile getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {
    if (!error) {
        self.imageView.image = [UIImage imageWithData:imageData];
    }
}];</p>

Are you sure the error is in setting the self.imageView.image property?.

because it says

Ribbit[465:60b] -[UIImageView objectForKey:]: unrecognized selector sent to instance 0x9d54d70.

UIImageView is not a Parse Object so it does not have objectForKey method.

Please check the line 465

No one of my files have more than 300 lines. Are you sure that number have relation with the lines of code?

The error point to a direction, 0x9d54d70, and this is the direction of the PFObject message

Please post the entire .h and .m files so I can see where the problem is :)

            <p>@interface ImageViewController : UIViewController

@property (nonatomic,strong) PFObject *message;
@property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end</p>
            ```

How you add a code here??

use triple thicks

`

Check the Markdown Cheatsheet below comment textarea :)

```@interface ImageViewController : UIViewController

@property (nonatomic,strong) PFObject *message; @property (weak, nonatomic) IBOutlet UIImageView *imageView;

@end

Ok you .h file seems fine.

PD: strong is the default in iOS 7, so it's optional to write it down.

// It's the same as (nonatomic, strong)
@property(nonatomic) PFObject * message; 
@interface ImageViewController ()

@end

@implementation ImageViewController

- (void)viewDidLoad
{
    [super viewDidLoad];

    PFFile *imageFile = [self.message objectForKey:@"file"];
    [imageFile getDataInBackgroundWithBlock:^(NSData *imageData, NSError *error) {
        if (!error) {
            self.imageView.image = [UIImage imageWithData:imageData];
        }
    }];


//PFFile *imageFile = [self.message objectForKey:@"file"];

//NSURL *imageFileUrl = [[NSURL alloc] initWithString:imageFile.url];

//NSData *imageData = [NSData dataWithContentsOfURL:imageFileUrl];

//self.imageView.image = [UIImage imageWithData:imageData];

    NSString *senderName = [self.message objectForKey:@"senderName"];
    NSString *title = [NSString stringWithFormat:@"Sent from %@", senderName];
    self.navigationItem.title = title;
}

@end

All seems quite right in .h and .m maybe the error is in another section of the app.

Please comment all your .m code in viewDidLoad and run the app.

All works fine if i comment all the .m code in viewDidLoad

Yes the code looks fine, maybe you have a problem with the storyboard IBOutlets.

Ben Jakuben
STAFF
Ben Jakuben
Treehouse Teacher

Perhaps self.message isn't being set properly. Can you verify that it has data? How does your prepareForSegue: method look in InboxViewController? Are you setting imageViewController.message like this below?

- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    if ([segue.identifier isEqualToString:@"showLogin"]) {
        [segue.destinationViewController setHidesBottomBarWhenPushed:YES];
    }
    else if ([segue.identifier isEqualToString:@"showImage"]) {
        [segue.destinationViewController setHidesBottomBarWhenPushed:YES];
        ImageViewController *imageViewController = (ImageViewController *)segue.destinationViewController;
        imageViewController.message = self.selectedMessage;
    }
}

Yes, I have the same code. I did it with the videos from the classes and i already reviewed many videos, but still cant figure out which is the error.

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Can you add a breakpoint on that line where you're setting imageFile? What does self.message look like if you hover over it while debugging? You can also print it in the debug console using "po self.message" (po stands for print object).

i added the breakpoint. This appear: (ImageViewController *) 0x9c385a0

It's Probably you are setting something wrong in the prepare for segue

What you got in this method?

- (void) prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender;

Can be possible to send my project file to you?

if ([segue.identifier isEqualToString:@"showLogIn"]) {
        [segue.destinationViewController setHidesBottomBarWhenPushed:YES];
    }
    else if ([segue.identifier isEqualToString:@"showImage"])
    {
        [segue.destinationViewController setHidesBottomBarWhenPushed:YES];
        ImageViewController *imageViewController = (ImageViewController *)segue.destinationViewController;
        imageViewController.message = self.selectedMessage;

    }

How do you set the self.selectedMessage property?

@property (nonatomic,strong) PFObject *selectedMessage;

where do you assign it's value?.

In this method on InboxViewController class:

-(void)tableView:(UITableView *)tableView didSelectRowAtIndexPath:(NSIndexPath *)indexPath
{
    self.selectedMessage = [self.messages objectAtIndex:indexPath.row];
    NSString *fileType = [self.selectedMessage objectForKey:@"fileType"];

    if ([fileType isEqualToString:@"image"])
    {
        [self performSegueWithIdentifier:@"showImage" sender:self];
    }
    else
    {

    }


}

if you can upload you project to a dropbox or similar I will see were is the problem.

The code seems all right, but I want to see in action :)

Thank you!, give your email to share the folder in dropbox

Please use share link

https://www.dropbox.com/help/167/en

As I though you have problems with your IBOutlets UIImageView was pointing to message rather than imageView property.

how i fix it ??

finally its working fine!!

THANK YOU for all the help and time.

Glad to read that, you are welcome.

please accept my answer so I can get the points :)