簡體   English   中英

邏輯錯誤“未定義或垃圾值返回給調用者”

[英]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不是12title是未初始化的。 您可以在第一行或switch語句的default情況下初始化它。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM