我正在下載一個NSManagedObject。將NSString分配給該對象的valueForKey @「username」。接下來,我將該字符串分配給UITableView中的cell.textLabel.text,並且出現異常。UITableView:cell.textLabel.text錯誤:[__NSArrayI isEqualToString:]
以下是這就是代碼拋出異常:
-(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:
UPDATE
的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
)
感謝。使用更新修改了問題。 – user1107173
它看起來像iPhone3GS是陣列中唯一的成員。在一瞬間看我更新的答案 – Justin
更新了答案。這應該是一個工作解決方案。我想確保你明白它是什麼以及它爲什麼起作用。這個答案僅僅是找到返回數組中的第一個對象。如果第一個對象不是NSString,則會失敗。如果是這種情況,則需要使用快速枚舉('for()'循環)迭代數組內容以找到數組中的每個對象。 – Justin