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

David Getchel
David Getchel
2,236 Points

Resizing an image Code Challenge (Task 1)

I'm stuck on this code challenge. Maybe someone can point me in the right direction.

"In this Latergram app, we only want to upload an image if the 'image' and 'caption' properties of the view controller below are set. In the 'upload' method, add a check to make sure that both properties have values before executing the block of code. Note: 'image' is a UIImage and 'caption' is an NSString."

I think I'm confused about "making sure both properties have values". And if I need to do something with LatergramViewController.h

These challenges have been much more difficult than before. This is what I have. I've tried many variations but not even sure if this is in the right direction.

if (self.image == nil && self.caption length] == 0){

}

Thanks, Dave

8 Answers

Ben Jakuben, I figured it out; pay close attention to the NOT of how the question is worded. Within the video we set up an if/else statement that we set the conditions to say IF the image and caption are set to nil/0 then do nothing but if else then do an action. What this question is saying is shortening the process by setting the condition to say IF NOT nil/0 then trigger code(!=) there is no need at this point to run the code within the else statement.

Thanks!

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Ah - you got it! I totally missed my own question when I checked it earlier. :)

I intentionally try to mix things up from the videos so you're not copying the same exact code, but I could see the confusion here. I will think about possibly changing the wording of the question. Let me know if you have any suggestions here. Maybe even just referencing that it's different from the videos.

Thanks!

if (self.image != nil && [self.caption length] != 0) {

    //make sure you include the code :) 
    CGSize newSize = CGSizeMake(320.0f, 480.0f);
    CGRect newRectangle = CGRectMake(0, 0, 320.0f, 480.0f);
    UIGraphicsBeginImageContext(newSize);
    }

Cheers,

.A

Alex Hedley
Alex Hedley
16,381 Points

Try a not (!) instead.

Currently your code would need to execute in the else block to fulfil the question.

This ever get answered? I feel like this code if it wasnt missing the "[" bracket then it would work just fine. I have tested in every variation that I can think of and I am still coming up with an error. Half of the time it shoes a code challenge engine error... wanted to check and see if the code is wrong or the code engine

David Getchel
David Getchel
2,236 Points

No I still haven't figured that one out. I need to go back and mess with it again.

Ben Jakuben, Can you review this? I am not sure what is going on. I have been stuck on this for two days. I have even tried to wait and look with fresh eyes this morning and still cant seem to get it right. I feel like it is because of something to do with the syntax of everything.

if (self.image == nil && [self.caption length] == 0){ //No Code }

//Rest of Code in Challenge

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Not sure what the problem is -- I will investigate!

Thanks! Please update me when you figure it out, I appreciate it!

I'm having this exact same issue. Has anyone else figured why this solution does not work?

Ben Jakuben
Ben Jakuben
Treehouse Teacher

Hi Jeremy,

We want to make sure that both the image and caption properties have values. If they do, then we run the existing code in the upload method.

This condition (from above) can be used to check if both the image and caption are blank. If this condition is met, then both are blank, and the code inside the curly braces will not be run:

if (self.image == nil && [self.caption length] == 0){ //No Code }

We would need to put code in the else condition here, and the else condition would run if both the image and caption are empty.

We can do this with one condition (avoiding the blank block of code and the else condition) by writing a check like this:

if (the image property is NOT nil AND the caption is NOT empty) { then do the code in the upload method

Negative logic like this can be a little clunky to read and write, but hopefully this answers your question.

Finally solved this today :D! My hint for this :

!=
J Kim
J Kim
15,218 Points

seems misleading because the way you implemented this in your video was to do the following =>

if (self.image == nil && [self.caption length] == 0){ } else { // Code here }

Which in my opinion, should still pass considering the fact it is the approach you took in the videos. Also at this point it's not even specified that you can use != operators in Objective-C. (Initially I tried !self.image == nil)