简体   繁体   English

Xcode“使用未声明的标识符”

[英]Xcode “Use of undeclared identifier”

I know many people ask this but all the answers are specific apps so I don't understand how to work it for my app. 我知道很多人问这个,但所有答案都是特定的应用程序,所以我不明白如何为我的应用程序工作。

        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;` 
    }

the reason being ut tableData variable is not retained and you have assigned it via factory method with is already autoreleased. 原因是没有保留ut tableData变量,你已经通过工厂方法分配了它已经自动释放了。

in .h file, make it retain property and use this variable with self. 在.h文件中,使其保留属性并将此变量与self一起使用。 in ur code. 在你的代码中。

@property(nonatomic,retain) NSArray *tableData;

in .m, 在.m,

@synthesize tableData;

then use it like: 然后使用它像:

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

now you wont get any error as tableData is now retained. 现在你不会得到任何错误,因为现在保留了tableData。

do not forget to release it in dealloc if you are not using ARC. 如果您不使用ARC,请不要忘记在dealloc释放它。

You have to declare your NSArray as a property and synthetize it. 您必须将NSArray声明为属性并将其合成。 In your class definition: 在你的班级定义中:

@property (retain, nonatomic) NSArray *tableData;

And in the implementation: 并在实施中:

@synthetize tableData = _tableData;

Alright then, this is a simple fix. 好吧那么,这是一个简单的修复。 You are receiving the error on every line that references "tableData" because it is undeclared. 您在引用“tableData”的每一行上都收到错误,因为它未声明。 In other words, your app was never told what "tableData" is. 换句话说,您的应用程序从未被告知“tableData”是什么。

You can declare "tableData" in your .h file, it should look something like this... 您可以在.h文件中声明“tableData”,它应该看起来像这样......

@interface yourClassName : UITableViewController 
{
    NSArray *tableData;
}

EDIT: Go with @grasGendarme's answer if you will only be calling for this array from within this function, if you wish to use it across the controller use this answer. 编辑:如果您只想在此函数中调用此数组,请使用@grasGendarme的答案,如果您希望在整个控制器中使用它,请使用此答案。

EDIT 2: In regards to your updated question, check for this on the line thats giving you the error. 编辑2:关于您更新的问题,请在给出错误的行上检查此问题。

在此输入图像描述

This blue arrow indicates that you have set a break point in your code on this line. 此蓝色箭头表示您已在此行的代码中设置了断点。 You can right click the error and select delete breakpoint. 您可以右键单击错误并选择删除断点。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM