无法识别的选择器发送到实例 0x10010c730->目标 C

发布于 2024-11-08 23:52:36 字数 882 浏览 1 评论 0原文

我是编程新手。我看过这段代码。将派生类对象返回给基类。 这样基类就可以指向派生类的方法。 这里 B 类中的静态函数将其对象返回到基类 班级。

基派生类.m

#import <Foundation/Foundation.h>
#import "B.h"
int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    [B p]; 
    [pool drain];
    return 0;
}

A.h

#import <Foundation/Foundation.h>


@interface A : NSObject {

}
@end

A.m

#import "A.h"

@implementation A
 @end

B.h

#import <Foundation/Foundation.h>
#import "A.h"

@interface B : A {

}
+(A*)p;
-(void)display;
@end

B.m

#import "B.h"


@implementation B
+(A*)p
{
    NSLog(@"returning derived class object to the base class!!");
    return [B new];
}
-(void)display
{
    NSLog(@"Hello");
}


@end

I am new to programming.I have seen this code.returning a derived class object to the base class.
So that the base class can then point to the derived class methods.
Here a static function in class B is returning its object to the base
class.

base-derivedclass.m

#import <Foundation/Foundation.h>
#import "B.h"
int main (int argc, const char * argv[]) {
    NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
    [B p]; 
    [pool drain];
    return 0;
}

A.h

#import <Foundation/Foundation.h>


@interface A : NSObject {

}
@end

A.m

#import "A.h"

@implementation A
 @end

B.h

#import <Foundation/Foundation.h>
#import "A.h"

@interface B : A {

}
+(A*)p;
-(void)display;
@end

B.m

#import "B.h"


@implementation B
+(A*)p
{
    NSLog(@"returning derived class object to the base class!!");
    return [B new];
}
-(void)display
{
    NSLog(@"Hello");
}


@end

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(2

往昔成烟 2024-11-15 23:52:36

p 是一个类方法。在 Obj-C 中,您可以通过在方法声明中使用 + 来表示类方法,并使用 - 来表示实例方法。您可以使用以下方法调用类方法:

// [ClassName methodName];
[B p];

或者您可以通过以下方式将 p 更改为实例方法:

- (A *)p;
// and call
// [instanceName methodName];
[dep p];

您可以检查 Objective-C:入门 开始使用这些内容。

p is a class method. In Obj-C you denote class method by using + in method declaration and - to denote instance method. You can call the class method by using this:

// [ClassName methodName];
[B p];

Or you can change p to instance method by this:

- (A *)p;
// and call
// [instanceName methodName];
[dep p];

You can check Objective-C: A Primer to get started with these.

剪不断理还乱 2024-11-15 23:52:36

这里的内存管理也存在一些混乱:

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
B* der = [[B alloc]init]; 
[der p];
[pool drain];

您创建并耗尽自动释放池,以便将内容放入其中(创建后),然后立即处理它们(当您耗尽它们时)。但你还没有向池中放入任何东西。您已经使用“alloc”创建了“der”,这意味着您“拥有”它用于内存管理目的,而不是将其放入自动释放池中,在那里它将被自动处理。

如果我所做的只是让你更加困惑,你可能应该看看一些关于 Objective-C 的入门书籍。他们都在某个时候涵盖了这个主题。或者您可以查看 Apple 有关内存管理的文档,但他们假设您已经了解某些事情。 (这很令人困惑,所以要有耐心......)

There's also some confusion in your memory management, here:

NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
B* der = [[B alloc]init]; 
[der p];
[pool drain];

You create and drain autorelease pools in order to put things into them (after creation) and then dispose of them right away (as you drain them). But you haven't put anything into the pool. You've used "alloc" to create "der," which means you "own" it for memory management purposes, as opposed to its being put in an autorelease pool where it will be taken care of automatically.

If all I've done is confuse you more, you should probably check out some introductory book on Objective-C. They all cover this topic at some point. Or you could look at Apple's docs on memory management, but they assume you already know certain things. (And it IS confusing, so be patient...)

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