1

I am new to programming in general and especially iOS. Also, this is my first time asking on StackOverflow. I hope I don't start by pasting the code incorrectly.

In my application, I need to load and show some JSON into an application.

I managed to follow a tutorial and adapt it to my needs, and so I can currently load a list of data into a ListView but I am having some trouble showing the entire post in a Details View.

I have a class for my Post ViewController (lists) and another for my Details ViewController (details).

This is what the problem part of the code looks so far (PostsViewController):

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

    if ([[segue identifier] isEqualToString:@"showDetailView"]) {

        NSIndexPath *ip = [self.tableView indexPathForSelectedRow];

        [[segue destinationViewController] getPost:[postsArray objectAtIndex:ip.row]];

    }
}

The method it calls is in the DetaisViewController:

- (void)getPost:(id)postObject {

    currentPost = postObject;

}

I have no idea what I'm doing wrong and to this moment I still do not have that deep understanding of the inner workings of Obj-C, compilers and such.

I am doing this as a test for an entry level job, and I expect to learn a little more day after day.

Also, the entire error is this:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFNumber length]: unrecognized selector sent to instance 0xb000000000000083'

Please let me know if I need to present more information and I apologise in advance for my noobness.

Regards!

  • 1
    The name `getPost` is confusing. It's rather `setPost`. The error message simply says that somewhere an `NSNumber` object is passed instead of an expected `NSString` object. Set the exception breakpoint to figure out where the error occurs. – vadian Jul 05 '16 at 15:41
  • in other words, probably somewhere in `currentPost` you expect something to be a string and it's actually a number (so it was a number in the JSON instead of a string) – Wain Jul 05 '16 at 15:48
  • To add exception breakpoint: https://developer.apple.com/library/ios/recipes/xcode_help-breakpoint_navigator/articles/adding_an_exception_breakpoint.html – danh Jul 05 '16 at 16:00

1 Answers1

1

Thanks everybody.

This was the problem, I had a method for the DetailsView that would change the Labels accordingly. It was set like this:

- (void)setLabels {

    lblPostTitle.text = currentPost.postTitle;
    lblPostBody.text = currentPost.postBody;
    lblPostId.text = currentPost.postId;
}

When debugging, I first tried to NSLog the currentPost.postTitle/Body/Id and it worked. So I realised that in the JSONplaceholder, postID was a number (http://jsonplaceholder.typicode.com/comments?postId=1).

So once I treated the number, everything worked out just fine.

- (void)setLabels {

    lblPostTitle.text = currentPost.postTitle;
    lblPostBody.text = currentPost.postBody;

    NSString* pi = currentPost.postId;
    lblPostId.text = [NSString stringWithFormat: @"Post ID: %@", pi];
}

And now it works!

Anyway I appreciate all comments and will keep working hard to avoid those rookie mistakes!

Kind regards!