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 Build a Photo Browser iPhone App View Controller Transitions Implementing Full Screen Transitions

Crash on DetailViewController when viewDidLoad finishes

After thoroughly testing my implementation code of the detail view controller (Photo Bomber app), I get a crash at the point when the viewDidLoad finishes.

See line 45 in my code: https://www.dropbox.com/s/k6be3t5if34dxla/Line_45_crash_viewDidLoad_finishes.png

If I execute my program after the breakpoint preceding the } finishing the code section for the viewDidLoad method, the app crashes with this message: https://www.dropbox.com/s/4ykwohdaj0vv6u5/Crash_Transition_when_ViewDidLoad_Finishes.png

Also, here is my property declaration in my header: https://www.dropbox.com/s/h7866jbhbq6s5e0/header_THDetailViewController.png

I NSLogged all of my dictionaries to highlight the data flow and to ensure the getter and setter methods where properly executed.

Everything seems to work great.

Not so sure why I am getting this crash right after viewDidLoad ends.

I would value the point of views of more experienced coders.

Thanks/Merci!

9 Answers

Stone Preston
Stone Preston
42,016 Points

just took a quick glance at your meta data controller. looking at this code:

@implementation THMetaDataView

- (void)setPhotoIG:(NSDictionary *)photoIG {

    _photoIG = photoIG;

    //Set the time

//    NSDate *createdAt = [NSDate dateWithTimeIntervalSince1970:[photoIG[@"created_time"] doubleValue]];
//    [self.timeButton setTitle:[createdAt sam_briefTimeInWords] forState:UIControlStateNormal];

    //Set the avatar for picture selected

    [THIGController igStandardResolutionForPhoto:_photoIG completion:^(UIImage *image) {
        self.avatarImageView.image = image;
    }];

    //Set the username

    [self.usernameButton setTitle:_photoIG[@"username"] forState:UIControlStateNormal];

    //Set the number of likes

    [self.likesButton setTitle:_photoIG[@"likes"][@"count"] forState:UIControlStateNormal];

    //Set the number of comments

    [self.commentsButton setTitle:_photoIG[@"comments"][@"count"] forState:UIControlStateNormal];


}

when you set the titles of the likes and the comments to the count of those, try formatting them. the count might be an NSNumber.

//Set the number of likes but format the string

    [self.likesButton setTitle:[NSString stringWithFormat:@"%@", _photoIG[@"likes"][@"count"]] forState:UIControlStateNormal];

    //Set the number of comments but format the string

    [self.commentsButton setTitle:[NSString stringWithFormat:@"%@", _photoIG[@"comments"][@"count"]]  forState:UIControlStateNormal];

if you have anywhere else in your code where you set the text using the count try changing those as well.

Stone Preston
Stone Preston
42,016 Points

more than likely you passed an NSNumber where the code was expecting an NSString. whats the type of the self.ig property?

Hi Stone: self.ig is a NSDictionary

Stone Preston
Stone Preston
42,016 Points

what does that igStandardResolutionFor photo look like. can you post that?

Stone Preston
Stone Preston
42,016 Points

also if you want just zip up your project and put it on dropbox and ill take a look at it locally.

Here you go Stone: https://www.dropbox.com/sh/h3dq0w4pz498kbe/AAAv3zRpTkD-mAmiw36Na5Sua

That way you'll have the whole thing.

igStandardResolutionForPhoto is a method that has a dictionary as argument and goes get the Standard Resolution Instagram of the corresponding tapped cell in the View Controller preceding the detail view controller displaying that standard resolution photo.

+ (void)igStandardResolutionForPhoto:(NSDictionary *)ig completion:(void(^)(UIImage *image))completion {
    if (ig == nil || completion == nil) {
        return;
    }

    NSLog(@"Data on ig: %@", ig);

    // *key is the Cache key and needs to have a unique identifier
    NSString *key = [[NSString alloc] initWithFormat:@"ig-id-%@", ig[@"id"]];

    NSURL *url = [[NSURL alloc] initWithString:ig[@"images"][@"standard_resolution"][@"url"]];
    [self downloadURL:url key:key completion:completion];
}


#pragma mark - Private

+ (void)downloadURL:(NSURL *)url key:(NSString *)key completion:(void(^)(UIImage *image))completion {
    UIImage *image = [[SAMCache sharedCache] imageForKey:key];
    if (image) {
        completion(image);
        return;
    }

    NSURLSession *session = [NSURLSession sharedSession];
    NSURLRequest *request = [[NSURLRequest alloc] initWithURL:url];
    NSURLSessionDownloadTask *task = [session downloadTaskWithRequest:request completionHandler:^(NSURL *location, NSURLResponse *response, NSError *error) {
        NSData *data = [[NSData alloc] initWithContentsOfURL:location];
        UIImage *image = [[UIImage alloc] initWithData:data];
        [[SAMCache sharedCache] setImage:image forKey:key];

        dispatch_async(dispatch_get_main_queue(), ^{
            completion(image);
        });
    }];
    [task resume];
}

Stone: you rock!

While you are at it and you have my code, could you have a quick look to see if you find (in 10 sec I am sure) the reason why my custom checkmark on the first UICollectionView no longer shows up?

I am trying to use Chris' vectorial approach ( see http://stackoverflow.com/questions/18977527/how-do-i-display-the-standard-checkmark-on-a-uicollectionviewcell ) blended with Andrej's concise one ( see https://github.com/pronebird/CollectionViewCheckbox/tree/master/CollectionViewCheckmark ).

I am pretty sure it is because I don't initialize properly. And I am struggling between initializing the IBOutlet or an instance of the SSCheckMark class. The latter shows the checkmarks but I can only select one (need NSMutable Set from Andrej's code I guess...)

Any ideas more than welcome, I am stuck, stuck, stuck!

:)

Stone Preston
Stone Preston
42,016 Points

im about to head out so I dont have time to check it out now, but I might have some time to look it over later. ill let you know if I find anything if I get the chance.

formatting the count fixed the crash correct?

Stone: Great if you can have a look later. Please let me know.

Yes, it was as simple as formatting the count. Pretty incredible... Thanks so much!

Eliezer Marte
Eliezer Marte
4,728 Points

How do you cache this data so it works in airplane mode?