Hi,

What I have works fine now, but to explain:

 Basically I have just a Class definition, e.g.

Class                   theClass;

I can't instantiate the class (not that I'd consider that anyway), because I have no idea what side effects calling the init method of the class would be, it might, for instance, spawn off a millions worker threads or it might be a Singleton that can't be released and should never have been created in the first place! So, I can't call any Instance Methods of the Class, but, I need to know info about the class in order cache it and later combine it with other data, e.g. I take the data returned from the Class Method chain and use this along with other data to form a value. This system is already in place and I've seen it used a number of times, I think even in Apple Sample code. I think CoreData does something like this although I may be mistaken.

This is performed once at Initialization (App Launch) time.

The object chain may or may not be created at some later point, if it does need to be, then another method in the same class as above gets called, it then uses information provided to the Method and Data Stored from the initialization to build the object corresponding Class chain.

So, in a Class called something like DataManager, there are two methods:

-(void) configureClass:(Class) theClass
{
DataManagerInfo*                        myInfo;

myDict = [theClass newDict];
if (myDict == nil)
        return;

myInfo = [[DataManagerInfo alloc] init];
[self computeInfoUsing:myInfo withSomeOtherData:otherData andConfigDict:myDict];

//  Store Info in Dictionary for later
[self.pClassDict setObject:myInfo forKey:NSStringFromClassName (theClass)];
}

-(id) createObjectForClass:(Class) theClass andOtherData:someOtherData
{
// takes info from pClassDict and data from OtherData and uses this to create the Object and returns it.
}


This all works fine and I think it's quite neat, it's certainly very fast, since a lot is pre-computed. The only problem I had was in some code in one of the newly added classes that called the super class like so:

[[super class] newDict];

Instead of [super newDict]

It was late and I was tired when I looked at the code and [[super class] newDict]; doesn't seem that unreasonable to expect to call the super class, which it did, but I hadn't realized that it in turn would call back up the chain. Then someone suggested:

[[self superclass] newDict];

Which also works, but so does [super newDict],

I not really sure which is the best in this case? I really don't want any class calling back up the chain in this case, so I think [[self superclass] newDict]; ?

I've got it working ok with [super newDict] but not sure if it would be best to change it to [[self superclass] newDict]

I don't want to hard code the class names into the code. Because I need it to work regardless of the order which I have no control over. The code that calls the DataManager class, has a network of class inheritance chains and hard coding (not that I can) would break that, for example:

BaseClass

BaseClass <--- TypeAClass

TypeAClass <--- TypeBClass

TypeBClass <--- TypeCClass

TypeCClass <--- TypeDClass

In this case 5 classes are configured and might created, BaseClass, A, B, C and D.

If someone changed it so that they inherited in a different order the code would break, by using super it avoids this problem.

On 12 Mar 2013, at 08:44, Graham Cox wrote:
All you need is:

@interface DictionaryFactory1 : NSObject

+ (NSDictionary*) dictionary;

@end

// repeat the above with additional class names as desired, e.g. DictionaryFactory2, DictionaryFactory3

+ (NSDictionary*)  dictionary
{
        // create the dictionary

        // set some preloaded data

        // return it
}

then your code just calls [DictionaryFactory1 dictionary], [DictionaryFactor2 dictionary] etc.


what's so hard about that?

Nothing, but it doesn't do what I want it to!


(n.b. it's NSDictionary, not MSDictionary, you may have the wrong OS in mind).

Even if Dictionary factory 2 inherits from Dictionary factory 1 for some reason that isn't clear from your post,

From my original post:
Take the following example:

@interface BaseClass
+(NSMutableDictionary*) newDict;
@end

@implementation BaseClass
+(NSMutableDictionary*) newDict
{
return [[NSMutableDictionary alloc] init];
}
@class NewClass;
@interface NewClass : BaseClass
+(NSMutableDictionary*) newDict;
@end

#import "BaseClass.h"
@implementation NewClass

+(NSMutableDictionary*) newDict
{
NSMutableDictionary*    myDict;

myDict = [[super class] newDict]; //*** This was the problem, it now read [super newDict] as all is well.
[myDict setObject:@"Some Data" forKey:@"someKey"];

return myDict;
}

@end

How is the above not clear? Apart from the names I chose, I'd say:

@interface NewClass : BaseClass

Was a bit of a give away! lol

it doesn't change this at all. You just override +dictionary and return what you want. There is no reason to call super at all.

If you want DF2 to add some further items to DF1's implementation, then call super and add the extra items to the dict:

Except it doesn't do what the above does!

@implementation DF2
+ (NSDictionary*) dictionary
{
        NSMutableDictionary* md = [[super dictionary] mutableCopy];
        // add extra items to md
        return [md autorelease];
}

From reading the above, I cant see this is any better than what I have, it fact it copies the dictionary every time, which is not what is wanted as well as being slower.

Cheers
Dave






_______________________________________________

Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)

Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com

Help/Unsubscribe/Update your Subscription:
https://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com

This email sent to arch...@mail-archive.com

Reply via email to