0

我正在下载一个 NSManagedObject。将 NSString 分配给该对象的 valueForKey@"username"。接下来,我将该字符串分配给 UITableView 中的 cell.textLabel.text 并且出现异常。

以下是引发异常的代码:

-(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];

}

日志

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

现在在 PDReviewVC 中:

。H

@property (nonatomic, retain) NSString * recipientUserName;

.m

@synthesize recipientUserName;

... //cellForRowAtIndexPath

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

我在 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:

更新

NSLog 用于以下代码:

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;

NS日志:

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
)
4

2 回答 2

1

您的问题的答案就在您的错误消息中。:-)

它告诉你,你认为类型的对象NSString是真的NSArray。因此,NSArray 不会响应任何名为isEqualToString.

在您的 fetchUserName 方法中,您将一个对象转换为 NSString 并返回它,但显然您在这里得到了一个 NSArray。当您将此对象设置为标签的文本属性时,会在幕后询问当前字符串属性是否等于您要设置的那个。然后,错误。

尝试return在 fetchUserName 方法中将此行放在您之前:

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

然后使用此 NSLog() 的控制台结果修改您的问题,我们可以帮助您找出数组中的内容。

好的,在您返回用户名对象之前,请执行以下操作:

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;
于 2013-08-30T19:31:14.873 回答
1

当它实际上返回[recipientUser valueForKey:@"username"]一个. 删除强制转换并让代码正常工作,因此您实际上是从.NSStringNSArray(NSString*)recipientUser

要调试,我建议将您的日志记录更改为:

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]);
   }
}
于 2013-08-30T19:29:59.730 回答