user1107173
user1107173

Reputation: 10764

UITableView: cell.textLabel.text error: [__NSArrayI isEqualToString:]

I'm downloading an NSManagedObject. Assigning an NSString to valueForKey@"username" of that object. Next, I assign that string to a cell.textLabel.text in UITableView and I'm getting an exception.

The following is the code thats throwing an exception:

-(NSString *)fetchUserName
{
    if ([fetchedUserNameObject objectAtIndex:0]!= NULL)
    {
        recipientUser = (User*)(fetchedUserNameObject);
        NSString *userName = (NSString*)[recipientUser  valueForKey:@"username"];            
        return userName;
    }
 }

.....

      NSString *recipientUserName = [self fetchUserName]

......

- (IBAction)reviewButtonPressed:(UIBarButtonItem *)sender
{
    NSLog(@"Recipient UserName from Review Button %@", recipientUserName);

    PDReviewVC *modalVC = [self.storyboard instantiateViewControllerWithIdentifier:@"pdReview"];
    UINavigationController *navBar=[[UINavigationController alloc]initWithRootViewController:modalVC];

    modalVC.recipientUserName= self.recipientUserName;
    [self presentViewController:navBar animated:YES completion:NULL];

}

Log

2013-08-30 11:51:49.393 Time[1188:c07] Recipient UserName from Review Button (
        iphone3gs
    )

Now in PDReviewVC:

.h

@property (nonatomic, retain) NSString * recipientUserName;

.m

@synthesize recipientUserName;

... //cellForRowAtIndexPath

if (indexPath.section == 0)
{
    cell.textLabel.text = recipientUserName;
}

I get the following error on cell.textLabel.text = recipientUserName;:

2013-08-30 11:51:54.679 Time[1188:c07] -[__NSArrayI isEqualToString:]: unrecognized selector sent to instance 0xb889390
2013-08-30 11:53:53.303 Time[1188:c07] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSArrayI isEqualToString:]: unrecognized selector sent to instance 0xb889390'
*** First throw call stack:

UPDATE

NSLog for the following code:

if ([fetchedUserNameObject objectAtIndex:0]!= NULL)
{
    recipientUser = (User*)(fetchedUserNameObject);
    NSLog(@"User %@", recipientUser);
    NSString *userName = (NSString*)[recipientUser valueForKey:@"username"];
    NSLog (@"userName from fetchUserName: %@", userName);
    NSLog(@"%@", userName);

    return userName;

}
else return NULL;

NSLog:

2013-08-30 12:36:29.495 Time[1341:c07] User (
    "<User: 0xa55cd60> (entity: User; id: 0xa5a0ae0 <x-coredata://3B273CFB-1CAA-4FA0-95DC-BA9420219380-1341-000007F42366B21A/User/piphone3gs> ; data: <fault>)"
)
2013-08-30 12:36:29.496 Time[1341:c07] userName from fetchUserName: (
    iphone3gs
)
2013-08-30 12:36:29.496 Time[1341:c07] (
    iphone3gs
)

Upvotes: 0

Views: 1412

Answers (2)

Joel
Joel

Reputation: 16134

You are casting [recipientUser valueForKey:@"username"] as an NSString to get your code to work, when it is in fact returning an NSArray. Remove the cast (NSString*) and get the code working so you are in fact pulling a string rather than an array from recipientUser.

To debug, I'd suggest changing your logging to:

if ([fetchedUserNameObject objectAtIndex:0]!= NULL)
{
   recipientUser = (User*)(fetchedUserNameObject);
   NSArray *userNames = [recipientUser valueForKey:@"username"];
   for (int i=0; i < [userNames count]; i++) {
       NSLog(@"Username %i is %@", i, [userNames objectAtIndex:i]);
   }
}

Upvotes: 1

Daddy
Daddy

Reputation: 9035

The answer to your question is right in your error message. :-)

It's telling you that the object you think is of type NSString is really NSArray. Thus, NSArray does not respond to any method called isEqualToString.

In your fetchUserName method you are casting an object as NSString and returning it, but apparently you are getting an NSArray here. When you set this object to your label's text property, something goes on behind the scenes to ask if the current string property is equal to the one you're trying to set. Then, error.

Try placing this line before you return in the fetchUserName method:

NSLog(@"%@",username);
return username;

Then modify your question with the console results of this NSLog() and we can help you figure out what is inside the array.

Ok, before you return the username object, do this:

NSString *username;
id object = [recipient valueForKey:@"username"];
if ([object isKindOfClass:[NSString class]]) {
    username = (NSString *)object;
    return username;
} else if ([object isKindOfClass:[NSArray class]]) {
    NSArray *returnedArray = (NSArray *)object;
    if (returnedArray.count > 0) {
        id arrayMember = [returnedArray objectAtIndex:0];
        if ([arrayMember isKindOfClass:[NSString class]]) {
            username = (NSString *)arrayMember;
            return username;
        }
    }
}
return nil;

Upvotes: 1

Related Questions