Working on a chat application. Using TTTAttributedLabel in my custom Cell. So that i can detect links. I implemented that code in my custom cell.

    self.tttAttributedLabel=[[TTTAttributedLabel alloc] init];
    self.tttAttributedLabel.font = [UIFont systemFontOfSize:14.0];
    self.tttAttributedLabel.numberOfLines = 0;
    self.tttAttributedLabel.lineBreakMode = NSLineBreakByWordWrapping;
    self.tttAttributedLabel.linkAttributes = [NSDictionary dictionaryWithObject:[NSNumber numberWithBool:YES] forKey:(__bridge NSString *)kCTUnderlineStyleAttributeName];
    self.tttAttributedLabel.enabledTextCheckingTypes = NSTextCheckingTypeLink; 
    self.tttAttributedLabel.delegate = self;
    [self.contentView addSubview:self.tttAttributedLabel];

i am getting crash in below function. I dont know why there is crash. it seems like self.links is not a valid array.

  - (NSTextCheckingResult *)linkAtCharacterIndex:(CFIndex)idx
{
     NSEnumerator *enumerator = [self.links reverseObjectEnumerator]; //crash on this 
   NSTextCheckingResult *result = nil;
   while ((result = [enumerator nextObject])) {
    if (NSLocationInRange((NSUInteger)idx, result.range)) {
        return result;
    }
}
return nil;
}

Chat

1

There are 1 best solutions below

0
On

You should use the designated initializer, initWithFrame:, even if you pass in CGRectZero as the frame. You are using init, which will not perform the common initialization method that initializes the links property.