我正在下载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;
的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
)
答案 0 :(得分:1)
您正在[recipientUser valueForKey:@"username"]
投射NSString
以使代码正常工作,而实际上它正在返回NSArray
。删除强制转换(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]);
}
}
答案 1 :(得分:1)
您的问题的答案就在您的错误消息中。 : - )
它告诉您,您认为属于NSString
类型的对象实际上是NSArray
。因此,NSArray不响应任何名为isEqualToString
的方法。
在你的fetchUserName方法中,你将一个对象转换为NSString并返回它,但显然你在这里得到一个NSArray。当您将此对象设置为标签的文本属性时,后台会发生一些事情,询问当前字符串属性是否等于您尝试设置的属性。然后,错误。
尝试在fetchUserName方法中return
之前放置此行:
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;