Objective-C 将对象声明为与我声明的完全不同的类

发布于 2024-12-01 19:29:04 字数 1524 浏览 0 评论 0原文

在我编写的方法中,我声明了类“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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

玻璃人 2024-12-08 19:29:04

比如说,声明一个指针 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 that fred is an NSArray. It's what you assign to fred that counts. If you assign an NSDateFormatter, then that's what fred is.

最好是你 2024-12-08 19:29:04

您认为应用程序委托的导航控制器顶部有一个 ClassesViewController 的假设显然是有缺陷的。它的顶部有一个 HomeViewController 。您的结构看起来可能有点复杂,因为您在控制器类的其他部分中有一个按钮连接到某个操作,因此可能有很多原因。

首先要检查的事情:如果您以编程方式实例化按钮,那么您是否不小心将按钮添加到了错误的控制器?如果您使用图形界面设计器设计所有内容,但将其保存在单个 NIB 中,是否有可能您复制并粘贴了一个按钮,却忘记更改其连接的内容?

如果您最终可以使用多个导航控制器,您确定应用程序委托所持有的那个控制器当前在屏幕上吗?

Your assumption that the application delegate's navigation controller has a ClassesViewController on top is clearly flawed. It has a HomeViewController 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?

心碎的声音 2024-12-08 19:29:04

简而言之,类型转换和类型转换是 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.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文