xcode 在 iOS 中以编程方式实例化类

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/8259896/
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-14 22:32:31  来源:igfitidea点击:

Instantiate Class Programmatically in iOS

objective-ciosxcodeclassdesign-patterns

提问by Ignacio Oroná

I am working a big iOS project, the design is not a nice as I would like it to be, but I must stick to it. (life can be a bitch sometimes).

我正在从事一个大型 iOS 项目,设计并不像我希望的那样好,但我必须坚持下去。(生活有时可能是个婊子)。

The thing is that we have a Library that basically let's you browse a catalog. You have a filter, where you specify a certain search criteria, and you are presented with a list were you can press on the items that you are interested. When you press an item, you can see a more detailed description of it.

问题是我们有一个库,基本上可以让您浏览目录。您有一个过滤器,您可以在其中指定特定的搜索条件,并且您会看到一个列表,您可以点击您感兴趣的项目。当你按下一个项目时,你可以看到它的更详细的描述。

The company were a work for, sells this same software to many different companies that have different catalogs. The idea is that the Library has all the main functionality, and the project that use it, might in some way extend or completely override some of the given interfaces.

该公司是一个工作,将相同的软件出售给许多拥有不同目录的不同公司。这个想法是图书馆拥有所有主要功能,使用它的项目可能会以某种方式扩展或完全覆盖某些给定的接口。

To give you an example, imagine my library has 2 classes that manages 2 views. They would be "FilterViewController" and "DetailsViewControllers". In some place of the code this classes gets instantiated. It would look something like this

举个例子,假设我的库有 2 个管理 2 个视图的类。它们将是“FilterViewController”和“DetailsViewControllers”。在代码的某个地方,这些类被实例化。它看起来像这样

Class diagram schema

Class diagram schema

My approach is something like this:

我的方法是这样的:

ProjectA side

项目A方

// Here I configure the library
Library.FilterViewClass = ProjectAFilterViewController;
Library.DetailsViewClass = ProjectADetailViewController;

ProjectB side

项目B方

Library.FilterViewClass = ProjectBFilterViewController;
Library.DetailsViewClass = nil;

Library side

图书馆一侧

// Did the user configure the library?
if(self.FilterViewClass == nil){
    // I alloc the default ViewController
    myFilterViewController = [[FilterViewController alloc] init]; 
}else{
    // Here I would like to alloc the custom ViewController
    myFilterViewController = [[Library.FilterViewClass alloc] init]; // DaProblem!
}

The problem with that approach is that I actually don't know if it's possible to instantiate object programmatically. Or at least I don't know how. Maybe I am using the wrong approach, some direction would be appreciated. Txs in advance!

这种方法的问题在于我实际上不知道是否可以以编程方式实例化对象。或者至少我不知道如何。也许我使用了错误的方法,将不胜感激。提前发送!

回答by beryllium

To get class from string you can use this function

要从字符串中获取类,您可以使用此函数

Class cl = NSClassFromString(@"MyClass");

To get class of existing variable just call classmethod.

要获取现有变量的类,只需调用class方法。

Class cl = [obj class]; // assuming obj1 is MyClass

Now you can create instance of MyClass

现在您可以创建实例 MyClass

MyClass *myClass = (MyClass*)[[cl alloc] init];
...
[myClass release];

回答by tarmes

Use

myFilterViewController = [[[Library.FilterViewClass class] alloc] init]; 

You can also instantiate from a class name, should that be useful to you:

如果对您有用,您还可以从类名实例化:

id obj = [[NSClassFromString(@"MyClass") alloc] init];

回答by lorean

Class someClass = [Foo1 class];
Foo * someObject = [[someClass alloc] init];
[someObject bar];

Class someClass2 = [Foo2 class];
Foo * someObject2 = [[someClass2 alloc] init];
[someObject2 bar];

Interface+Implementation:

接口+实现:

@interface Foo : NSObject 
- (void)bar;
@end

@interface Foo1 : Foo

@end

@interface Foo2 : Foo
@end

@implementation Foo
- (void)bar {
    NSLog(@"abstract foo");
}
@end

@implementation Foo1
- (void)bar {
    NSLog(@"foo1bar");
}
@end

@implementation Foo2
- (void)bar {
    NSLog(@"foo2bar");
}
@end

Output:

输出:

2011-11-24 11:24:31.117 temp[21378:fb03] foo1bar
2011-11-24 11:24:31.118 temp[21378:fb03] foo2bar