如何分配动态类型对象

发布于 2024-07-13 19:27:44 字数 458 浏览 7 评论 0原文

我看过很多关于 Objective-C 中动态类型的讨论。 但我还没有看到任何我认为应该是什么的例子。

可以说我有一个通用函数,它应该处理两个对象(一个被分配,另一个被释放),并且调用对象将其自身附加到新分配的对象。 两者都是从 class0 继承的,

如果您认为它可以解释某些内容,请随意解释它!

如果在运行时选择该类,我如何处理参数列表(?现在是占位符) 我如何分配一个类在运行时才定义的对象?

-(void) juggle:(?*)objclass1:(?*)objclass2{

? temp = [? alloc] init];
objclass1 = temp;
[temp release];

[objclass2.view removefromsuperview];
[self.handle insertsubview:objclass1.view];

}

I have seen a lot of talk about dynamic typing in objective-c. But i haven't seen any examples of what i think it is supposed to be.

lets say I have a generic function that is supposed to juggle two objects (one gets allocated and the other gets freed) and the calling object attaches it self to the newly alloced object. Both are inherited from class0

Please feel free to interpret this however you want if you think it will explain something!!

If the class is picked at runtime, how do i deal with the arguments list (? is a placeholder for now)
How do i alloc a object who's class is not defined until runtime?

-(void) juggle:(?*)objclass1:(?*)objclass2{

? temp = [? alloc] init];
objclass1 = temp;
[temp release];

[objclass2.view removefromsuperview];
[self.handle insertsubview:objclass1.view];

}

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

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

发布评论

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

评论(2

晨曦慕雪 2024-07-20 19:27:44

我不知道你的代码试图做什么,它在语法上无效,并且操作视图与你的问题无关。 无论如何,如果你真的不知道类型,你通常使用“id”,它的类型转换为“void *”用于代码生成。 它有一个特殊的属性,即假设它接收任何消息,因此它不会触发未知消息的编译器警告。

为了实例化一个类,您只需要为其保存“Class”对象。 在 Objective C 中,类的所有实例都引用 Class 对象(遗留运行时中的 isa 指针),该对象也响应方法。 所以换句话来说,在下面的代码中:

NSArray *myObject = [[NSArray alloc] init];

NSArray 实际上是一个对象。 因此,这将生成等效的代码结果:

Class myClass = [NSArray class];
NSArray *myObject = [[myClass alloc] init];

或者甚至

Class myClass = NSClassFromString(@"NSArray");
NSArray *myObject = [[myClass alloc] init];

使用 NSClassFromString 函数,该函数会进入运行时并找到具有您传入的名称的类。

如果使用类 getter,则所有对象都会返回其类,因此要实例化一个对象,该对象是与现有对象相同的类,如下所示:

- (void) leakObjectWithSameClassAs:(id)object {
    [[[object class] alloc] init];
}

I have no idea what the code you have there is trying to do, it is not syntactically valid, and manipulating views has nothing to do with your questions. Anyway, if you really don't know the type you generally use "id" which is type cast to a "void *" for codegen. It has the special property that it is assumed to receive any message, so it does not trigger compiler warnings for unknown messages.

In order to instantiate a class you just need to be holding the "Class" object for it. In Objective C all instances of a class refer to a Class object (the isa pointer in the legacy runtime), which also responds to methods. So in other words, in the following code:

NSArray *myObject = [[NSArray alloc] init];

NSArray is actually an object. So this will generate equivalent code results:

Class myClass = [NSArray class];
NSArray *myObject = [[myClass alloc] init];

or even

Class myClass = NSClassFromString(@"NSArray");
NSArray *myObject = [[myClass alloc] init];

Which uses the function NSClassFromString which walks into the runtime and finds a class with the name you pass in.

All objects return their class if use the class getter, so to instantiate an object that is the same class as an existing object like this:

- (void) leakObjectWithSameClassAs:(id)object {
    [[[object class] alloc] init];
}
只是在用心讲痛 2024-07-20 19:27:44

这就是我现在所拥有的,

- (void)flipfromv1tov2:(UIViewController*)v1:(NSString*)nib1:(UIViewController*)v2{

    if(v1 == nil)
    {
        UIViewController *newview = [[[v1 class] alloc] initWithNibName:nib1 bundle:nil];
        v1 = newview;
        [newview release];
    }
    [v2.view removeFromSuperview];
    [self.view insertSubview:v1.view atIndex:0];    
}

我还无法验证它,因为我有链接问题...我将此函数添加到我的根控制器中,但由于某种原因,我收到一条警告,表明该函数是隐式声明的。 构建失败,因为函数调用永远不会链接到任何东西

This is what i have now

- (void)flipfromv1tov2:(UIViewController*)v1:(NSString*)nib1:(UIViewController*)v2{

    if(v1 == nil)
    {
        UIViewController *newview = [[[v1 class] alloc] initWithNibName:nib1 bundle:nil];
        v1 = newview;
        [newview release];
    }
    [v2.view removeFromSuperview];
    [self.view insertSubview:v1.view atIndex:0];    
}

I cannot verify it yet because I have a linking problem...I added this func to my root controller but for some reason I get a warning that the function is implicitly declared. And the build fails because the function call never get linked to anything

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