With generics on languages like C# or Java, you can have a factory that returns a result depending on the given type? For example you can tell the factory method to return:
对于c#或Java等语言的泛型,您可以有一个工厂,根据给定的类型返回结果?例如,您可以告诉factory方法返回:
Book
List<Book>
Door
List<Door>
Is it possible to achieve the same thing with objective-c? Can I somehow tell generateObjects
method to return me an array of books?
用objective-c也能达到同样的效果吗?我能否告诉generateObjects方法来返回一系列的书籍?
[self getDataFromWeb:@"SOME_URL" andReturnResultWithType:[Book class]];
// What about an array of Books?
- (id)getDataFromWeb:(NSString*)url andReturnResultWithType:(Class)class
{
// Convert JSON and return result
// Mapping conversion is a class method under each contract (Book, Door, etc)
}
Let's say this is one of my data contracts
假设这是我的一个数据契约
@interface Book : JSONContract
@end
@implementation Book
+ (NSDictionary *)dataMapping
{
// returns an NSDictionary with key values
// key values define how JSON is converted to Objects
}
@end
EDIT:
编辑:
Modified the examples to be more clear
修改示例以使其更清晰
2 个解决方案
#1
4
No, it is no possible to say that your array will contain String But, Yes, it is possible to create String based on a Class definition or even a class name.
不,不能说您的数组将包含字符串,但是,可以根据类定义甚至类名创建字符串。
Objective-C as "reflection" capabilities like Java, it is called "introspection"
Objective-C作为像Java一样的“反射”功能,被称为“内省”
For example, you can create an object based on its class name using this code
例如,您可以使用此代码基于类名创建一个对象
NSString* myString = (NSString*)[[NSClassFromString(@"NSString") alloc] init];
NSClassFromString is documented here : https://developer.apple.com/library/mac/#documentation/cocoa/reference/foundation/miscellaneous/foundation_functions/reference/reference.html
NSClassFromString在这里有文档记录:https://developer.apple.com/library/mac/#文档/cocoa/reference/foundation/ foundation_functions/reference/reference.html。
If you want the compiler to check types for you, you can also directly use the Class object, as this
如果希望编译器检查类型,还可以直接使用Class对象,如下所示
Class stringClass = [NSString class];
NSString* myString = [[stringClass alloc] init];
#2
1
Yes, NSArray and NSMutableArray store objects of type id, which means you can put whatever you want in there and return it to the user. You just check the parameter passed in to branch your logic for generating the objects you are putting in the array.
是的,NSArray和NSMutableArray存储id类型的对象,这意味着您可以在其中放置任何您想要的对象并将其返回给用户。您只需检查传递给分支逻辑的参数,以生成要放入数组的对象。
Your comment suggests this is for converting JSON? To convert JSON you must have a series of conditions checking if the value looks like a number, string, etc. So you could add a condition that says if the class parameter is NSString class then just assume the JSON value is a string.
你的评论建议这是用来转换JSON的?要转换JSON,您必须检查一系列条件,如果值看起来像数字、字符串等。因此,您可以添加一个条件,如果类参数是NSString类,那么只需假设JSON值是一个字符串。
#1
4
No, it is no possible to say that your array will contain String But, Yes, it is possible to create String based on a Class definition or even a class name.
不,不能说您的数组将包含字符串,但是,可以根据类定义甚至类名创建字符串。
Objective-C as "reflection" capabilities like Java, it is called "introspection"
Objective-C作为像Java一样的“反射”功能,被称为“内省”
For example, you can create an object based on its class name using this code
例如,您可以使用此代码基于类名创建一个对象
NSString* myString = (NSString*)[[NSClassFromString(@"NSString") alloc] init];
NSClassFromString is documented here : https://developer.apple.com/library/mac/#documentation/cocoa/reference/foundation/miscellaneous/foundation_functions/reference/reference.html
NSClassFromString在这里有文档记录:https://developer.apple.com/library/mac/#文档/cocoa/reference/foundation/ foundation_functions/reference/reference.html。
If you want the compiler to check types for you, you can also directly use the Class object, as this
如果希望编译器检查类型,还可以直接使用Class对象,如下所示
Class stringClass = [NSString class];
NSString* myString = [[stringClass alloc] init];
#2
1
Yes, NSArray and NSMutableArray store objects of type id, which means you can put whatever you want in there and return it to the user. You just check the parameter passed in to branch your logic for generating the objects you are putting in the array.
是的,NSArray和NSMutableArray存储id类型的对象,这意味着您可以在其中放置任何您想要的对象并将其返回给用户。您只需检查传递给分支逻辑的参数,以生成要放入数组的对象。
Your comment suggests this is for converting JSON? To convert JSON you must have a series of conditions checking if the value looks like a number, string, etc. So you could add a condition that says if the class parameter is NSString class then just assume the JSON value is a string.
你的评论建议这是用来转换JSON的?要转换JSON,您必须检查一系列条件,如果值看起来像数字、字符串等。因此,您可以添加一个条件,如果类参数是NSString类,那么只需假设JSON值是一个字符串。