Xcode“使用未声明的标识符”
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/11175938/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Xcode "Use of undeclared identifier"
提问by user1477809
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;`
}
回答by Saurabh Passolia
the reason being ut tableData
variable is not retained and you have assigned it via factory method with is already autoreleased.
原因是 uttableData
变量没有被保留,你已经通过工厂方法分配了它,并且已经自动释放了。
in .h file, make it retain property and use this variable with self. in ur code.
在 .h 文件中,使其保留属性并将此变量与 self 一起使用。在你的代码中。
@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.
dealloc
如果您不使用 ARC,请不要忘记释放它。
回答by toasted_flakes
You have to declare your NSArray as a property and synthetize it. In your class definition:
你必须将你的 NSArray 声明为一个属性并合成它。在您的类定义中:
@property (retain, nonatomic) NSArray *tableData;
And in the implementation:
在实施中:
@synthetize tableData = _tableData;
回答by Mick MacCallum
Alright then, this is a simple fix. You are receiving the error on every line that references "tableData" because it is undeclared. In other words, your app was never told what "tableData" is.
好吧,这是一个简单的修复。您在引用“tableData”的每一行上都会收到错误,因为它是未声明的。换句话说,您的应用程序从未被告知“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.
此蓝色箭头表示您在此行的代码中设置了一个断点。您可以右键单击错误并选择删除断点。