[英]Logic error “Undefined or garbage value returned to caller”
以下陳述有什么問題?
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSString *title;
switch (section)
{
case 0:
title = @"Section 1";
break;
case 1:
title = @"Section 2";
break;
default:
break;
}
return title;
}
為什么在分析此代碼時會出現“未定義或垃圾值返回給調用者”的邏輯錯誤?
將NSString * title設置為nil: NSString * title = nil;
if(section既不是0也不是1)然后switch(section)經歷默認值:然后它返回title; 這只是指針指向任何東西或未初始化的指針。 所以將標題字符串賦給nil; 你宣布它的地方。
- (NSString *)tableView:(UITableView *)tableView titleForHeaderInSection:(NSInteger)section
{
NSString *title = nil;
switch (section)
{
case 0:
title = @"Section 1";
break;
case 1:
title = @"Section 2";
break;
default:
break;
}
return title;
}
因為當section
不是1
或2
, title
是未初始化的。 您可以在第一行或switch
語句的default
情況下初始化它。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.