警告:「メッセージの受信者 'sizewithfont:constaredtosize:linebreakmode:' is nil…」
-
24-10-2019 - |
質問
私はこの警告を受けました:
「メッセージの受信者 'sizewithfont:constaredtosize:linebreakmode:'はnilであり、ゴミになるタイプ「cgsize」の値を返します」
理解できません。私は何が間違っているのですか?
これが私が使用しているコードです:
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath {
NSString *text = nil;
NSUInteger row = indexPath.row;
if (indexPath.section == FIRST_SECTION) {
text = [_firstArray objectAtIndex:row];
} else if (indexPath.section == SECOND_SECTION) {
text = [_secondArray objectAtIndex:row];
} else {
text = nil;
NSLog(@"Wrong section");
}
UITableViewCell *cell = [self myCell];
UILineBreakMode lineBreakMode = cell.textLabel.lineBreakMode;
CGFloat width = _tableView.contentSize.width - (kTableCellHPadding*2 + tableCellMargin*2);
UIFont* font = cell.textLabel.font;
CGSize size = [text sizeWithFont:font
constrainedToSize:CGSizeMake(width, CGFLOAT_MAX)
lineBreakMode:lineBreakMode];
if (size.height > kMaxLabelHeight) size.height = kMaxLabelHeight;
return size.height + kTableCellVPadding*2;
}
解決
原因は次のコードセグメントです。
if (indexPath.section == FIRST_SECTION) {
text = [_firstArray objectAtIndex:row];
} else if (indexPath.section == SECOND_SECTION) {
text = [_secondArray objectAtIndex:row];
} else {
NSLog(@"Wrong section");
}
その他の部分では、テキスト変数に値は割り当てられていません。だから、それはnilのままです。したがって、Xcodeはそれがnullであると不平を言っています。
所属していません StackOverflow