如何解决调用方法 [self somemethod] 时出现的错误
我有这样的代码:
// .m
- (void)viewDidLoad {
NSMutableArray *array = [[NSMutableArray alloc] init];
[self addToArray];
}
- (void)addToArray {
NSString *stringA;
[stringA isEqualToString:@"door"];
NSString *stringB;
[stringB isEqualToString:textField.text];
[array addObject:stringA];
if ([stringA isEqual:stringB]) {
[stringA isEqual:nil];
[tableView reloadData];
} else {
[array addObject:stringB];
[tableView reloadData];
}
}
当我调用方法 addToArray 时,它不断返回一个名为 Thread 1: Program recided signal "EXC_BAD_ACCESS"
的错误,并且调试器输出显示:单步执行,直到从函数退出objc_msgSend,没有行号信息。
位于[self addToArray]
行。知道如何解决吗?我已经浪费了很多时间,请帮助我!
I have this code:
// .m
- (void)viewDidLoad {
NSMutableArray *array = [[NSMutableArray alloc] init];
[self addToArray];
}
- (void)addToArray {
NSString *stringA;
[stringA isEqualToString:@"door"];
NSString *stringB;
[stringB isEqualToString:textField.text];
[array addObject:stringA];
if ([stringA isEqual:stringB]) {
[stringA isEqual:nil];
[tableView reloadData];
} else {
[array addObject:stringB];
[tableView reloadData];
}
}
When I call the method addToArray it keeps returning me an error called Thread 1: Program recived signal "EXC_BAD_ACCESS"
, and the debugger output says : Single stepping until exit from function objc_msgSend, which has no line number information.
at the line [self addToArray]
. Any idea of how to solve it? I have wasted to much time with it, please help me!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
正如其他人所说,
array
应该是类的实例变量或属性,在.h
文件中声明:或者,没有ARC:
现在你
@合成数组;
在您的实现文件中,并且可以从任何地方访问它。然后你可以这样做:你似乎假设 isEqualToString 做了一个赋值。它不会,它检查字符串的(文本)相等性。试试这个:
As was said by others,
array
should be an instance variable or property of the class, declared in the.h
file:Or, without ARC:
Now you
@synthesize array;
in your implementation file and can access it from anywhere. Then you can do:You seem to assume that
isEqualToString
does an assignment. It doesn't, it checks strings for (textual) equality. Try this:这两个变量未初始化,会给您带来大问题:
您没有为 stringA 或 stringB 分配任何内容。此外,您调用 isEqualToString 的结果永远不会被使用。
These two variables are uninitialized and will cause you big problems:
You have not assigned anything to either stringA or stringB. Besides the result of your call to isEqualToString is never used.
我可以在您的代码中注意到两件事:
1)将
array
设为类变量,以便您可以从-[addToArray]
方法访问它。最好在 .h 文件中执行此操作,例如:然后,在 .m 文件中,该方法应如下所示:
并且不要忘记释放数组:
2) 不要搞乱
-[NSString isEqualToString: ]
方法,对变量进行简单的赋值。例如,在您的-[addToArray]
方法中,将 this: 替换为 this:
And this:
替换为 this:
3) 检查
-[addToArray]
方法的逻辑 -目前还不清楚你要实现什么目标。Two things I can notice in your code:
1) Make
array
a class variable, so you can access it from your-[addToArray]
method. Better do this in your .h file, for example:Then, in your .m file the method should look like this:
And don't forget to release the array:
2) Do not mess up
-[NSString isEqualToString:]
method with simple assigment to a variable. So in your-[addToArray]
method, for example, replace this:with this:
And this:
with this:
3) Check the logic of
-[addToArray]
method - it is not very clear what are you going achieve.