1

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

Gagan Joshi
  • 3,347
  • 2
  • 21
  • 32
  • I would put a break point at `- (void)setLinks:(NSArray *)links` and then do a back trace to see what is called prior. From there work back until you figure out where `self.links` is being set. – James Nelson Aug 14 '14 at 19:24
  • did you figure out a solution? I'm having the same problem and the links NSArray seems to be losing reference – kevinl Jan 12 '15 at 21:01
  • @kevinl Not to plug my own answer here, but the issue (as I mention below) is that `init` is not the correct designated initializer; you must use `initWithFrame:`. Otherwise, the links property will not be initialized. – Jonathan Hersh Jan 21 '15 at 02:31
  • @JonathanHersh Thanks Jonathan. However, I've been using initWithFrame. – kevinl Jan 21 '15 at 17:42

1 Answers1

0

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.

Jonathan Hersh
  • 1,073
  • 1
  • 8
  • 15