Why isn't this the right class?

Discussion in 'iOS Programming' started by ArtOfWarfare, Dec 17, 2009.

  1. ArtOfWarfare macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #1
    Code:
    for (i = 0; i < [classIDs count]; i++)
    {
    	NSLog(@"Adding a %i section", i);
    	[sections addObject:[[NSMutableArray alloc] init]];
    			
    	if ([[[sections objectAtIndex:i] class] isKindOfClass: [[sections objectAtIndex:i] class]])
    	{
    		NSLog(@"It said it was right before...");
    	}
    	else
    	{
    		NSLog(@"The test sucks...");
    	}
    }
    I have "The test sucks..." returned every time.

    This either means Apple's class and isKindOfClass: methods are poorly written or I'm misusing them.

    I suspect I'm misusing them.

    Can someone please explain them to me?

    And a follow up... I actually don't want to check to see if an object is of its own class, I want to check to see
    Code:
    if ([[[sections objectAtIndex:section] class] isKindOfClass: [NSMutableArray class]])
    if the object is an NSMutableArray like it should be. Because later in my code when I try telling it to check its contents, it ends up crashing with the error that some class other class was sent a method intended for an NSMutableArray.

    ... here's that code too, I guess, seeing as I can't figure it out either.

    Code:
    - (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
    {
    	NSLog(@"%@", [classNames objectForKey:[[[sections objectAtIndex:section] objectAtIndex:0] objectForKey:@"ClassID"]]);
    	return [classNames objectForKey:[[[sections objectAtIndex:section] objectAtIndex:0] objectForKey:@"ClassID"]];
    }
    When it returns an error when crashing, it says the issue pertains to objectAtIndex being sent to an object that doesn't recognize it. The worse part is, it seems to vary from build to build what it's sending it to. Sometimes it says UniqueIndexPath or something like that was sent it. I can't remember the others and I didn't write them down unfortunately. Oh, and then most of the time it simply crashes without logging a single error...

    it'll print the titles of the headers like the NSLog tells it to, but there's nothing after that and the app has crashed.

    So... help?

    1.) I can't figure out how to use class and isKindOfClass: properly...
    2.) Periodically the object in an array seems to be of the wrong class and thus incapable of responding to a method I send it...

    Edit:
    Okay, sometimes it ends up returning
    Furthermore, I replaced the whole "isKindOfClass:" test with a "respondsToSelector" test... and the result is, it NSMutableArray when the object is initially created and added, but later fails.

    This makes me wonder if maybe the system also has a variable called sections? IDK...
    help!

    The truly baffling part is that the NSLog immediately before still works.

    I've modified it a little more... now it does this:
    Code:
    NSString *titleHeader = [NSString stringWithFormat:@"%@", [classNames objectForKey:[[[sections objectAtIndex:section] objectAtIndex:0] objectForKey:@"ClassID"]]];
    NSLog(@"%@", titleHeader);
    return titleHeader;
    
    The NSLog STILL displays the correct title.
    And afterwards I get this error:
    I am getting so frustrated... why isn't it working?

    Edit 2X:
    Without changing a single line of code and building it again, I got this now:
    Edit 3X:
    And now CALayer has received it...

    Edit 4X:
    And now UIControlTargetAction has received it...
     
  2. robbieduncan Moderator emeritus

    robbieduncan

    Joined:
    Jul 24, 2002
    Location:
    London
    #2
    isKindOfClass: is an instance method of NSObject. So you should be passing it to an object instance, not the Class object. It's single parameter, however, is a Class object.

    So, as a concrete example, you probably want

    Code:
    if ([[sections objectAtIndex:i]isKindOfClass: [[sections objectAtIndex:i] class]])
     
  3. ArtOfWarfare thread starter macrumors 604

    ArtOfWarfare

    Joined:
    Nov 26, 2007
    #3
    Based on the lines of code that seem to cause the problem...
    there seems to be an issue with an assignment querying the contents of an array that's inside another array.

    Asking for the count of the objects inside an array that's inside another array seems to work just fine, thus my table view will create the correct number of sections and rows in each section, but it won't name either the sections or the rows correctly.

    :confused:

    I'm so horribly confused why my code isn't working.
     
  4. PhoneyDeveloper macrumors 68030

    PhoneyDeveloper

    Joined:
    Sep 2, 2008
    #4
    Forget about isKindOfClass. A simple NSLog(@"%@", object) will give you the info you need.

    The cause of your 'unrecognized selector sent to instance' errors is either 1) bad memory management practices (you're overreleasing the objects) or 2) you really are mistaking one object for another.

    You should stop forthwith writing code with 28 nested brackets. It makes it impossible to debug problems like those in (2) above.

    NSZombies can help you track down issues related to (1) above.
     

Share This Page