objective-c 将类作为参数传递?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/1030605/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 21:45:52  来源:igfitidea点击:

Pass a class as a parameter?

objective-ciphonecocoa

提问by JoBu1324

I have been lead to believe that it is possible to pass a class as a method parameter, but I'm having trouble implementing the concept. Right now I have something like:

我一直认为可以将类作为方法参数传递,但是我在实现这个概念时遇到了麻烦。现在我有类似的东西:

- (id)navControllerFromView:(Class *)viewControllerClass
                          title:(NSString *)title
                      imageName:(NSString *)imageName
{
    viewControllerClass *viewController = [[viewControllerClass alloc] init];
    UINavigationController *thisNavController =
    [[UINavigationController alloc] initWithRootViewController: viewController];

    thisNavController.tabBarItem = [[UITabBarItem alloc]
                                  initWithTitle: title
                                          image: [UIImage imageNamed: imageName]
                                            tag: 3];
    return thisNavController;
}

and I call it like this:

我这样称呼它:

rootNavController = [ self navControllerFromView:RootViewController
    title:@"Contact"
    imageName:@"my_info.png"
];

What's wrong with this picture?

这张图有什么问题?

回答by Peter Hosey

- (id)navControllerFromView:(Class)viewControllerClass
- (id)navControllerFromView:(Class)viewControllerClass

It's just Class, without the asterisk. (Classisn't a class name; you're not passing a pointer to an instance of the Classclass, you're passing the class itself.)

它只是Class,没有星号。(Class不是类名;您不是传递指向Class类实例的指针,而是传递类本身。)

rootNavController = [ self navControllerFromView:RootViewController
rootNavController = [ self navControllerFromView:RootViewController

You can't pass a bare class name around like this—you have to send it a message. If you actually want to pass the class somewhere, you need to send it the classmessage. Thus:

你不能像这样传递一个空的类名——你必须给它发送一条消息。如果你真的想在某个地方传递类,你需要向它发送class消息。因此:

rootNavController = [self navControllerFromView:[RootViewController class]
                                          title:@"Contact"
                                      imageName:@"my_info.png"
];

回答by Ben Gotow

It looks like you've almost got it. You want to pass [RootViewController class]instead of RootViewController. That gives you a Classvalue. Also, I don't think you want your function to take a Class *, just a Class. It's not actually an Objective-C object.

看起来你已经差不多了。你想通过[RootViewController class]而不是RootViewController. 这给了你一个Class价值。另外,我认为您不希望您的函数采用Class *,而只是采用Class. 它实际上不是一个 Objective-C 对象。

回答by Steven Canfield

You haven't explained any problem with the code you've posted. What doesn't work? What are you expecting to happen? The only thing I can think of so far is that you might want to pass [RootViewController class]instead of just RootViewController. Also, your formatting is killing me.

您还没有解释您发布的代码有任何问题。什么不起作用?你期待发生什么?到目前为止,我唯一能想到的是您可能想要通过[RootViewController class]而不是RootViewController. 此外,您的格式正在杀死我。

回答by Adam Johns

In Swift:

在斯威夫特:

func acceptingFunction(aClass: AnyClass) {
    ... 
}

call with:

致电:

acceptingFunction(UIView)