Objective-C:你能测试静态类类型的未初始化指针吗? [英] Objective-C: can you test an uninitialized pointer for static class type?

查看:148
本文介绍了Objective-C:你能测试静态类类型的未初始化指针吗?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想在某些对象A中执行以下行为:

I'm trying to do something along the lines of:

UITableVIewController *viewController;
[someObjectB setupView: viewController];



在某些对象B中:



in some object B:

-(void) setupView:(UIViewController*)view
{
    if the pointer passed in is of type UITableViewController then ...
    say, for instance with: [[view class] isKindOfClass [UITableViewController class]]
    or: [view isKindOfClass [UITableViewController class]]
}

$但是,我有两个问题:
首先,参数在B中输入UIViewController的方法,UIViewController是UITableView的超类(我这样做,因为这个方法也将配置其他类型的视图控制器)。所以我想知道,如果我传递UITableViewController到类型UIViewController的方法参数,isKindOfClass仍然是动态绑定吗?也就是说,运行时检查内容的类型,而不考虑指针的静态类型?

However, I have two problems: First, the fact that the parameter enters the method in B as UIViewController which is a superclass of UITableView (which I'm doing because this method will also configure other types of view controllers). So I'm wondering, if I pass in UITableViewController to a method parameter of type UIViewController, isKindOfClass still does dynamic binding right? That is, the runtime checks the type of the content, regardless of the static type of the pointer?

其次,注意我没有在传递之前初始化对象A中的指针它到B.我其实想让B来初始化它。但是后来买的问题变成了,我如何测试一个未初始化的指针的静态指针类型?也就是说,在B:[view isKindOfClass [UITableViewController class]]给我一个BAD_EXEC。我的猜测,因为我显然试图访问一个jul指针。所以有一种方法仍然测试基于指针静态类型声明?

Second, notice I did not initialize the pointer in object A before passing it to B. I actually want B to initialize it. But then buy question becomes, how do I test the static pointer type on an uninitialized pointer? That is, inside B: [view isKindOfClass [UITableViewController class]] gives me a BAD_EXEC. My guess, because I'm obviously trying to access a jul pointer. So is there a way to still test based on the pointer static type declaration?

让我以更具体的术语来重新说明这个问题的部分:我想要obj B创建一个类型为UITableViewController的变量,将它传递给A,并且实现它被赋予一个指针UITableViewController ,所以它可以实际初始化这个指针到UITableViewController的一个特定的子类,然后obj B传递,现在初始化的对象到一些其他对象。

Let me rephrase that part of the question in more specific terms: I want obj B to create a variable of type UITableViewController, pass it to A and have A realize it's been given a pointer to UITableViewController, so that it can actually initialize this pointer to a specific subclass of UITableViewController and then obj B pass that now initialized object to some other object.

我想要这样做的推理是以下场景:
我有一个基于navigationController的应用程序。它有一个带有按钮的根控制器,并且根据哪个按钮被按下,根控制器实例化并推动用于数据库的不同表的表控制器,并且当点击这些表的行时,表实例化并推送详细视图控制器。

My reasoning behind wanting to do this is the following scenario: I have a navigationController-based app. It's got a root controller with buttons and depending which button is pressed, the root controller instantiates and pushes table controllers for different tables of a database and, in turn, when clicking on a row of those tables, the tables instantiate and push a detailed view controller.

到目前为止很好,但是因为项目的性质,我想保持整个导航控制器实例化 - 推链不可知的事实,有一个数据库并且表和详细视图控制器实际上分别是UITableViewController和UIViewController的特定子类。

So far so good, but because of the nature of the project, I want to keep the whole navigationcontroller instantiate-push chain agnostic to the fact that there's a database and that the tables and detailed-view controllers are actually specific subclasses of UITableViewController and UIViewController, respectively.

这意味着流程将是:

1-点击后,root控制器将通用UIViewController传递给我的自定义主控制器。

1- On click, root controller passes generic UIViewController to my custom main controller.

2-这个主控制器意识到它已经被传递一个通用表,初始化它到我定义的一个特定的子类,并做一些事情提供数据

2- This main controller realizes it's been passed a generic table, initializes it to a specific subclass defined by me and does a few things to provide it with data from the sql database.

3 - 根控制器,不关心通用的UIViewController是否是一个表,甚至是UIViewController的子类,只是将它推送到navigationController 。

3 - The root controller, not caring whether the generic UIViewController is a table or not or even a subclass of UIViewController, merely pushes it to the navigationController.

这个想法也是,我不需要在根控制器中包含表子类的头。只有自定义主控制器知道这些。

The idea, also, is that then I don't need to include headers for the table subclasses in the root controller. Only the custom main controller knows about these.

所以,在一天结束时,我看到的做法是,通过能够确定什么是类型的指针被传递,不管这个指针是否为nil,指向垃圾或指向一个实际的对象。含义:

So, at the end of the day, the way I see of doing it is by being able to determine what is the type of the pointer being passed, regardless of whether this pointer is nil, points to garbage or points to an actual object. Meaning:

UIVIewController * a - >在编译时确定的静态类型声明。

UIVIewController* a -> static type declaration, determined at compile time.

a = [some子类UIVIewController alloc] init]; - >动态类型的指针内容,在运行时确定,可能通过以下链接通过激活记录..类似于虚拟在C ++

a = [some subclass of UIVIewController alloc] init]; -> dynamic type of the pointer's content, determined at run time, probably by following links through activation records.. similar to "virtual" in C++

推荐答案

正如其他人所指出的,你要求的是不可能的。当你传递一个变量到一个方法,它只发送值,而不是类型。此外,运行时不知道变量在方法中的类型。任何变量类型的处理都是由编译器完成的。

As others have pointed out, what you are asking for is impossible. When you pass a variable to a method, it only sends the value, not the type. Also, the runtime has no knowledge of a variable's type within a method. Any variable type handling is done by the compiler. The runtime can only know the class of an object which a variable holds.

或者,如果你可以改变你的方法的签名,你可以添加一个参数,指示要创建的对象的类型。调用方法仍然不需要知道对象的确切类型,只是它是否是一个表视图控制器。下面是一个使用枚举来保存不同类型的示例。

As an alternative, if you can change the signature of your method, you could add a parameter which indicates the type of object to create. The calling method still doesn't need to know the exact type of the object, just whether it is a table view controller or not. Here is an example which uses an enumeration to hold the different types.

// header

enum ViewControllerType {
    ViewControllerNormal = 0,
    ViewControllerTableView
};
- (void)setupView:(UIViewController **)view ofType:(enum ViewControllerType)type;

// implementation

- (void)setupView:(UIViewController **)view ofType:(enum ViewControllerType)type {
    if(!view) {
        NSLog(@"setupView:ofType: received nil pointer");
        return;
    }
    switch(type) {
        case ViewControllerNormal:
            // create normal UIViewController type
            break;
        case ViewControllerTableView:
            // create UITableViewController type
            break;
        default:
            NSLog(@"setupView:ofType: received unknown type: %i",type);
            return;
    }
}

使用方式:

UIViewController *view;
[creatorObject setupView:&view ofType:ViewControllerNormal];
// or
UITableViewController *tableView;
[creatorObject setupView:&tableView ofType:ViewControllerTableView];

这篇关于Objective-C:你能测试静态类类型的未初始化指针吗?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆