Objective-C 将对象声明为与我声明的完全不同的类
在我编写的方法中,我声明了类“A”的实例并调用它的方法。在运行时,当该方法运行时,我的应用程序崩溃了。它说尽管我声明了“A”的实例,但仍将无法识别的选择器发送到“B”类的实例。我在某处读到,我可能没有正确管理内存,因此它将方法发送到另一个类,但我正在使用 ARC,所以这根本不应该成为问题。非常感谢您的帮助!
The error I'm getting:
2011-08-27 01:25:49.859 Intelligenda[49385:bc03] PVC: <HomeViewController: 0x59359e0>
2011-08-27 01:25:49.945 Intelligenda[49385:bc03] -[HomeViewController addNewClass:]: unrecognized selector sent to instance 0x59359e0
2011-08-27 01:25:49.947 Intelligenda[49385:bc03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[HomeViewController addNewClass:]: unrecognized selector sent to instance 0x59359e0'
以及正在调用的方法:
-(IBAction)done:(id)sender{
[teacherName resignFirstResponder];
[className resignFirstResponder];
IntelligendaAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
UINavigationController *nav = delegate.navController;
[nav popViewControllerAnimated:YES];
ClassesViewController *classesParentViewController = (ClassesViewController *) nav.topViewController;
ClassIG *theNewClass = [[ClassIG alloc] init];
theNewClass.className = className.text;
theNewClass.teacherName = teacherName.text;
NSIndexPath *indexPath;
theNewClass.subject = [tableView cellForRowAtIndexPath:indexPath].textLabel.text;
// add reminder to array
NSLog(@"PVC: %@", [classesParentViewController description]);
[classesParentViewController addNewClass:theNewClass];
}
In a method I wrote, I'm declaring an instance of class 'A' and calling a method on it. At run time, when the method runs, my app crashes. It says an unrecognized selector was sent to an instance of class 'B' even though I declared an instance of 'A'. I read somewhere that I may not be managing my memory correctly so it's sending the method to another class, but I'm using ARC so that shouldn't even be a problem. Help would be much appreciated!
The error I'm getting:
2011-08-27 01:25:49.859 Intelligenda[49385:bc03] PVC: <HomeViewController: 0x59359e0>
2011-08-27 01:25:49.945 Intelligenda[49385:bc03] -[HomeViewController addNewClass:]: unrecognized selector sent to instance 0x59359e0
2011-08-27 01:25:49.947 Intelligenda[49385:bc03] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[HomeViewController addNewClass:]: unrecognized selector sent to instance 0x59359e0'
and the method that's being called:
-(IBAction)done:(id)sender{
[teacherName resignFirstResponder];
[className resignFirstResponder];
IntelligendaAppDelegate *delegate = [[UIApplication sharedApplication] delegate];
UINavigationController *nav = delegate.navController;
[nav popViewControllerAnimated:YES];
ClassesViewController *classesParentViewController = (ClassesViewController *) nav.topViewController;
ClassIG *theNewClass = [[ClassIG alloc] init];
theNewClass.className = className.text;
theNewClass.teacherName = teacherName.text;
NSIndexPath *indexPath;
theNewClass.subject = [tableView cellForRowAtIndexPath:indexPath].textLabel.text;
// add reminder to array
NSLog(@"PVC: %@", [classesParentViewController description]);
[classesParentViewController addNewClass:theNewClass];
}
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
比如说,声明一个指针
NSArray* fred
不会执行任何操作(除了少量的编译时警告消息)以确保fred
是一个NSArray
>。重要的是您分配给fred
的内容。如果您分配一个NSDateFormatter
,那么这就是fred
。Declaring, say, a pointer
NSArray* fred
does nothing (other than a sprinkling of compile-time warning messages) to assure thatfred
is anNSArray
. It's what you assign tofred
that counts. If you assign anNSDateFormatter
, then that's whatfred
is.您认为应用程序委托的导航控制器顶部有一个 ClassesViewController 的假设显然是有缺陷的。它的顶部有一个
HomeViewController
。您的结构看起来可能有点复杂,因为您在控制器类的其他部分中有一个按钮连接到某个操作,因此可能有很多原因。首先要检查的事情:如果您以编程方式实例化按钮,那么您是否不小心将按钮添加到了错误的控制器?如果您使用图形界面设计器设计所有内容,但将其保存在单个 NIB 中,是否有可能您复制并粘贴了一个按钮,却忘记更改其连接的内容?
如果您最终可以使用多个导航控制器,您确定应用程序委托所持有的那个控制器当前在屏幕上吗?
Your assumption that the application delegate's navigation controller has a
ClassesViewController
on top is clearly flawed. It has aHomeViewController
on top. Your structure looks like it may be a little complicated, given that you have a button somewhere wired up to an action in some other part of your controller classes, so there could be a bunch of causative reasons.First things to check: if you're instantiating buttons programmatically then did you accidentally add the button to the wrong controller? If you're designing everything using the graphical interface designer but keeping it in a single NIB, is it possible you copied and pasted a button and forgot to change what it's wired to?
If you can end up with multiple navigation controllers, are you sure the one held by the application delegate is currently on screen?
简而言之,类型转换和类型转换是 objc 中程序员的一项练习。
在这里阅读我的答案:
通过NSNumber* 到 NSString* 预期参数不会导致编译器警告/错误
...有关如何声明/转换变量的更多信息对象与其他语言的不同之处,以及如何检测这些情况。
the short answer is that typecasting and type conversions are an exercise for the programmer in objc.
read my answer here:
Passing NSNumber* to NSString* expected-parameter does not cause compiler warning / error
...for some more information as to how declaring/casting a variable of an object differs from other languages, and how you can detect those cases.