Ios JSQMessagesViewController-设置日期和消息时发生崩溃
我正在使用JSQMessagesViewController从服务器发送和接收消息。消息的显示是正确的,但当试图显示日期时(日期随所有消息一起出现),JSQMessageTimeStampFormatter库类就会崩溃。哪个是-(NSAttributedString*)attributedTimestampForDate:(NSDate*)date 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因是:“nsConcreteModableAttributedString initWithString:attributes::nil value”Ios JSQMessagesViewController-设置日期和消息时发生崩溃,ios,objective-c,jsqmessagesviewcontroller,Ios,Objective C,Jsqmessagesviewcontroller,我正在使用JSQMessagesViewController从服务器发送和接收消息。消息的显示是正确的,但当试图显示日期时(日期随所有消息一起出现),JSQMessageTimeStampFormatter库类就会崩溃。哪个是-(NSAttributedString*)attributedTimestampForDate:(NSDate*)date 由于未捕获的异常“NSInvalidArgumentException”而终止应用程序,原因是:“nsConcreteModableAttribut
- (NSAttributedString *)attributedTimestampForDate:(NSDate *)date
{
if (!date) {
return nil;
}
NSString *relativeDate = [self relativeDateForDate:date];
NSString *time = [self timeForDate:date];
//在时间戳的下行中获取崩溃
NSMutableAttributedString *timestamp = [[NSMutableAttributedString alloc] initWithString:relativeDate
attributes:self.dateTextAttributes];
[timestamp appendAttributedString:[[NSAttributedString alloc] initWithString:@" "]];
[timestamp appendAttributedString:[[NSAttributedString alloc] initWithString:time
attributes:self.timeTextAttributes]];
return [[NSAttributedString alloc] initWithAttributedString:timestamp];
}
我的代码
//获取客户聊天信息
- (void)getCustomerChatMessages
{
[[ServiceManager sharedInstance]GetChatMessagesforParentSK:self.str_ChatMessageSK completionBlock:^(id result, NSError *error)
{
if (!error)
{
NSDictionary *recallsDict = (NSDictionary *)result;
self.array_CustChatMessage = [recallsDict valueForKey:@"ChatMessageData"];
for (NSArray *dict in self.array_CustChatMessage)
{
JSQMessage * copyMessage = [[JSQMessage alloc] initWithSenderId:[[dict valueForKey:@"FromSK"] stringValue]
senderDisplayName:[dict valueForKey:@"FirstName"]
date:[dict valueForKey:@"DateCreated"]
text:[dict valueForKey:@"Message"]];
[self.messages addObject:copyMessage];
}
dispatch_async(dispatch_get_main_queue(), ^{
[self finishSendingMessageAnimated:YES];
});
}
}];
}
- (NSAttributedString *)collectionView:(JSQMessagesCollectionView *)collectionView attributedTextForCellTopLabelAtIndexPath:(NSIndexPath *)indexPath
{
/**
* This logic should be consistent with what you return from `heightForCellTopLabelAtIndexPath:`
* The other label text delegate methods should follow a similar pattern.
*
* Show a timestamp for every 3rd message
*/
{
JSQMessage *message = [self.messages objectAtIndex:indexPath.item];
NSLog(@"\n\n\n Message Date :%@",message.date);
return [[JSQMessagesTimestampFormatter sharedFormatter] attributedTimestampForDate:message.date];
}
return nil;
}
- (NSAttributedString *)collectionView:(JSQMessagesCollectionView *)collectionView attributedTextForMessageBubbleTopLabelAtIndexPath:(NSIndexPath *)indexPath
{
JSQMessage *message = [self.messages objectAtIndex:indexPath.item];
/**
* iOS7-style sender name labels
*/
if ([message.senderId isEqualToString:self.senderId])
{
return nil;
}
if (indexPath.item - 1 > 0)
{
JSQMessage *previousMessage = [self.messages objectAtIndex:indexPath.item - 1];
if ([[previousMessage senderId] isEqualToString:message.senderId]) {
return nil;
}
}
/**
* Don't specify attributes to use the defaults.
*/
NSLog(@"Sender Display Name:%@",[[NSAttributedString alloc] initWithString:self.strSenderName]);
return [[NSAttributedString alloc] initWithString:self.strSenderName];
}
您是否尝试从服务器响应中检查
date
,是不是为零?@Evgeny Karkan date来自服务器,我选中,NSLog(@“\n\n\n消息日期:%@”,Message.date);好的,尝试定位异常发生的位置-添加异常断点,下一步-检查nil
@EvgenyKarkan的可能方法参数我已经更新了问题。我正在GetSign carsh NSMutableAttributedString*时间戳=[[NSMutableAttributedString alloc]initWithString:relativeDate属性:self.dateTextAttributes];什么是nil-relativeDate
或self.dateTextAttributes
?我猜某些参数是零…您是否尝试从服务器响应中检查date
,是否为零?@Evgeny Karkan date来自服务器,我选中,NSLog(@“\n\n\n消息日期:%@”,Message.date);好的,尝试定位异常发生的位置-添加异常断点,下一步-检查nil
@EvgenyKarkan的可能方法参数我已经更新了问题。我正在GetSign carsh NSMutableAttributedString*时间戳=[[NSMutableAttributedString alloc]initWithString:relativeDate属性:self.dateTextAttributes];什么是nil-relativeDate
或self.dateTextAttributes
?我猜一些参数是零。。。