簡體   English   中英

Xcode“使用未聲明的標識符”

[英]Xcode “Use of undeclared identifier”

我知道很多人問這個,但所有答案都是特定的應用程序,所以我不明白如何為我的應用程序工作。

        tableData = [NSArray arrayWithObjects:@"Chocolate Brownie", @"Mushroom Risotto", nil];
    }

    - (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection: (NSInteger)section
    {
        return [tableData count];
    }

    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath
    {
        static NSString *simpleTableIdentifier = @"SimpleTableItem";

        UITableViewCell *cell = [tableView dequeueReusableCellWithIdentifier:simpleTableIdentifier];

        if (cell == nil) {
            cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:simpleTableIdentifier];
        }

        cell.textLabel.text = [tableData objectAtIndex:indexPath.row];
        return cell;` 
    }

原因是沒有保留ut tableData變量,你已經通過工廠方法分配了它已經自動釋放了。

在.h文件中,使其保留屬性並將此變量與self一起使用。 在你的代碼中。

@property(nonatomic,retain) NSArray *tableData;

在.m,

@synthesize tableData;

然后使用它像:

self.tableData = [NSArray arrayWithObjects:@"Chocolate Brownie", @"Mushroom Risotto",      nil];

現在你不會得到任何錯誤,因為現在保留了tableData。

如果您不使用ARC,請不要忘記在dealloc釋放它。

您必須將NSArray聲明為屬性並將其合成。 在你的班級定義中:

@property (retain, nonatomic) NSArray *tableData;

並在實施中:

@synthetize tableData = _tableData;

好吧那么,這是一個簡單的修復。 您在引用“tableData”的每一行上都收到錯誤,因為它未聲明。 換句話說,您的應用程序從未被告知“tableData”是什么。

您可以在.h文件中聲明“tableData”,它應該看起來像這樣......

@interface yourClassName : UITableViewController 
{
    NSArray *tableData;
}

編輯:如果您只想在此函數中調用此數組,請使用@grasGendarme的答案,如果您希望在整個控制器中使用它,請使用此答案。

編輯2:關於您更新的問題,請在給出錯誤的行上檢查此問題。

在此輸入圖像描述

此藍色箭頭表示您已在此行的代碼中設置了斷點。 您可以右鍵單擊錯誤並選擇刪除斷點。

暫無
暫無

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

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