Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/24.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/9/ios/93.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 在iOS中以编程方式实例化类_Objective C_Ios_Xcode_Class_Design Patterns - Fatal编程技术网

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

Objective c 在iOS中以编程方式实例化类,objective-c,ios,xcode,class,design-patterns,Objective C,Ios,Xcode,Class,Design Patterns,我正在做一个大的iOS项目,设计并不像我希望的那样好,但我必须坚持下去。(生活有时可能是个婊子) 问题是我们有一个图书馆,基本上可以让你浏览目录。您有一个过滤器,在其中指定特定的搜索条件,并显示一个列表,您可以按您感兴趣的项目。按下某个项目时,可以看到该项目的更详细说明 这家公司是一家为之工作的公司,向许多拥有不同目录的不同公司销售相同的软件。其思想是,库具有所有主要功能,而使用它的项目可能以某种方式扩展或完全覆盖某些给定接口 举个例子,假设我的库有两个类,管理两个视图。它们将是“FilterV

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

问题是我们有一个图书馆,基本上可以让你浏览目录。您有一个过滤器,在其中指定特定的搜索条件,并显示一个列表,您可以按您感兴趣的项目。按下某个项目时,可以看到该项目的更详细说明

这家公司是一家为之工作的公司,向许多拥有不同目录的不同公司销售相同的软件。其思想是,库具有所有主要功能,而使用它的项目可能以某种方式扩展或完全覆盖某些给定接口

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

我的方法是这样的:

项目A侧

// Here I configure the library
Library.FilterViewClass = ProjectAFilterViewController;
Library.DetailsViewClass = ProjectADetailViewController;
Library.FilterViewClass = ProjectBFilterViewController;
Library.DetailsViewClass = nil;
项目B侧

// Here I configure the library
Library.FilterViewClass = ProjectAFilterViewController;
Library.DetailsViewClass = ProjectADetailViewController;
Library.FilterViewClass = ProjectBFilterViewController;
Library.DetailsViewClass = nil;
库端

// 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!
}
这种方法的问题是,我实际上不知道是否有可能以编程方式实例化对象。或者至少我不知道怎么做。也许我使用了错误的方法,一些方向将被感激。提前发送Txs

使用

myFilterViewController = [[[Library.FilterViewClass class] alloc] init]; 
如果对您有用,您还可以从类名实例化:

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

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

Class cl = NSClassFromString(@"MyClass");
要获取现有变量的类,只需调用
class
方法

Class cl = [obj class]; // assuming obj1 is MyClass
现在您可以创建
MyClass

MyClass *myClass = (MyClass*)[[cl alloc] init];
...
[myClass release];
接口+实现:

@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
输出:

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

Txs大副,这正是a想要的!
id
instanceof
能否像这样解决所有动态类解析问题?有什么已知的问题吗?另外,这是一个糟糕设计的例子吗?