Programming class programs in iOS

I am working on a large iOS project, the design is not as pleasant as we would like, but I have to stick with it. (sometimes life can be a bitch).

The fact is that we have a library that basically allows you to browse the directory. You have a filter in which you specify certain search criteria, and you are presented with a list that you can click on the items of interest to you. When you click an item, you can see a more detailed description of it.

The company worked, sells the same software to many different companies that have different directories. The idea is that the library has all the basic functionality, and a project that uses it can somehow extend or completely redefine some of these interfaces.

To give an example, imagine that in my library there are 2 classes that control two views. They will be "FilterViewController" and "DetailsViewControllers". At some point in the code, these classes are instantiated. It will look something like this.

Class diagram schema

My approach looks something like this:

ProjectA side

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

ProjectB side

 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 this approach is that I really don't know if it is possible to programmatically create an object. Or at least I don’t know how to do it. Maybe I'm using the wrong approach, some directions will be appreciated. Txs in advance!

+7
source share
3 answers

To get a class from a string, you can use this function

 Class cl = NSClassFromString(@"MyClass"); 

To get the class of an existing variable, just call the class method.

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

Now you can create an instance of MyClass

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

Using

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

You can also create an instance from the class name if this is useful to you:

 id obj = [[NSClassFromString(@"MyClass") alloc] init]; 
+6
source
 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 
+5
source

All Articles