سؤال


Skip the following

These values are the first three lines of a TVC didSelectRowAtIndexPath method which is giving me all kinds of exceptions when I select an item in a row.

How do I:

a. Check that the below dictionary, array and selectedCategory have values.

b. Ensure that they do if they turn out not to?

NSDictionary *dictionary = [listOfItems objectAtIndex:indexPath.section];
NSArray *array = [dictionary objectForKey:@"News"];
NSString *selectedCategory = [array objectAtIndex:indexPath.row];

To here :

Exception from UIView tableView:numberOfRowsInSection:]: unrecognized selector sent to instance 0x7193a60

هل كانت مفيدة؟

المحلول

Here is a simple adjustment. There are normally better ways to store data than nesting all these data collections, but this is a simple drop in replacement for what you have already that should stop the crashes.

// First check your listOfItems
NSDictionary *dictionary = NULL;
if (listOfItems.count > indexPath.section) {
    dictionary = [listOfItems objectAtIndex:indexPath.section];
}

// Next check your dictionary isn't null.
NSArray *array = NULL;
if (dictionary) {
    array = [dictionary objectForKey:@"News"];
}

// Next check your array and make sure your array has values.
NSString *selectedCategory = NULL;
if (array.count > indexPath.row) {
   selectedCategory = [array objectAtIndex:indexPath.row];
}

// Finally do your stuff
if (selectedCategory) {
    // Do your stuff
} else {
    // Something went wrong along the way and you don't have a selectedCategory string.
}

نصائح أخرى

It seems that initWithString method of NSPlaceholderString got a nil as an argument. So , to figure when this exactly happens, it would be good to add an exception breakpoint. Take a look here

To check that they have values you can check...

[dictionary count];

[array count];

[selectedCategory length];

or you can check things like...

dictionary == nil;

array == nil;

[selectedCategory isEqualToString:@""];

selectedCategory == nil;

There are lots of ways. Not exactly sure what you are asking but you can check these.

مرخصة بموجب: CC-BY-SA مع الإسناد
لا تنتمي إلى StackOverflow
scroll top