1

I am using JSQMessagesViewController for sending and receiving message from Server. Messages are displaying proper, But when trying to display date,which s coming along with all message, then getting crash on JSQMessagesTimestampFormatter Library Class. which is - (NSAttributedString *)attributedTimestampForDate:(NSDate *)date

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: 'NSConcreteMutableAttributedString initWithString:attributes:: nil value'

- (NSAttributedString *)attributedTimestampForDate:(NSDate *)date
{
 if (!date) {
    return nil;
}

NSString *relativeDate = [self relativeDateForDate:date];
NSString *time = [self timeForDate:date];

//Getting Crash in Below Line for TimeStamp

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

My Code

//Get Customer Chat Message

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

}
ChenSmile
  • 3,401
  • 4
  • 39
  • 69
  • Have you tried to check `date` from server response, is it nil not? – Evgeny Karkan Jul 18 '16 at 11:49
  • @Evgeny Karkan date is comign from server, i cheked, NSLog(@"\n\n\n Message Date :%@",message.date); – ChenSmile Jul 18 '16 at 11:54
  • Ok, try to locate a place where exception occurs - add an exception breakpoint, next - check possible method parameters for `nil` – Evgeny Karkan Jul 18 '16 at 12:01
  • @EvgenyKarkan i have updated question . i m gettign carsh NSMutableAttributedString *timestamp = [[NSMutableAttributedString alloc] initWithString:relativeDate attributes:self.dateTextAttributes]; – ChenSmile Jul 18 '16 at 12:11
  • What is nil - `relativeDate` or `self.dateTextAttributes`? I guess some parameter is nil... – Evgeny Karkan Jul 18 '16 at 12:17
  • @EvgenyKarkan yes relativeDate s getting nil . but i m getting crash in library method. – ChenSmile Jul 18 '16 at 12:19
  • Let us [continue this discussion in chat](http://chat.stackoverflow.com/rooms/117606/discussion-between-imran-and-evgeny-karkan). – ChenSmile Jul 18 '16 at 12:45
  • hiii @ChenSmile i need help in deleting message when clicked on MenuItem "delete" currently when i click on delete it crashes could u help me to solve this – Dilip Tiwari Jul 28 '18 at 06:14

0 Answers0