[OSX-Users] Vaguely Cocoa related programming question...

Daniel Thorpe dt05r at ecs.soton.ac.uk
Mon Mar 23 15:05:56 GMT 2009


Hi everyone,

I'm wondering if someone can suggest a solution to the following  
problem, it's a bit involved, so apologies in advance :)

I'm writing an Objective-C protocol as part of a framework, lets call  
it MyObjectProtocol. Objects that conform to the protocol must also  
define a collection of other objects, called MyObjectProperty. So we  
basically have something like this...

@protocol MyObjectProtocol
+ (NSDictionary *)availableProperties;
+ (MyObjectProperty *)propertyForKey:(MyObjectPropertyKey)k;
- (CGFloat)valueForPropertyKey:(MyObjectPropertyKey)propKey;
@end

And an example of a class conforming to the protocol might look like  
this (only included the important bits):

typedef enum MyObjectPropertyKey {
	kTrignometricModelSineProperty = 1,
	kTrignometricModelCosineProperty = 2,
	kTrignometricModelTangentProperty = 3,
} TrignometricModelPropertyKeys;

@interface TrignometricModel : NSObject <MyObjectModel> {
	NSInteger len;
	float *buffer;	
	float *sine;
	float *cosine;
	float *tangent;
}

@end


#import "TrignometricModel.h"


@implementation TrignometricModel

+ (NSDictionary *)availableProperties {
	static NSDictionary *availableProperties;
	if(!availableProperties) {
		availableProperties = [[NSDictionary alloc] initWithObjectsAndKeys:
					// Sine property
					[DTGraphDatasourceProperty defaultPropertyWithName:@"Sine"  
andKey:kTrignometricModelSineProperty],
					[NSNumber numberWithInteger:kTrignometricModelSineProperty],
					// Cosine Property
					[DTGraphDatasourceProperty propertyWithName:@"Cosine"  
andKey:kTrignometricModelCosineProperty],
					[NSNumber numberWithInteger:kTrignometricModelCosineProperty],
					// Tangent Property
					[DTGraphDatasourceProperty propertyWithName:@"Tangent"  
andKey:kTrignometricModelTangentProperty],
					[NSNumber numberWithInteger:kTrignometricModelTangentProperty],	 
nil];		
	}
	return availableProperties;
}

+ (MyObjectProperty *)propertyForKey:(MyObjectPropertyKey)k {
	return [[TrignometricModel availableProperties] objectForKey: 
[NSNumber numberWithInteger:k]];
}

I'm not sure what's happened to the colour syntax there, however, the  
problem is that I want a default property key, as other objects in the  
framework need to pass those property keys between functions. So  
initially i thought I could define a base enum type containing a  
default (this is the MyObjectPropertyKey type), and then in the class  
that conforms to the protocol, that enum could be extended to contain  
the custom keys. This way the rest of the framework can use the base  
enum type in method defininitions.

However, I'm not sure that extending an enum is possible (in C). Does  
anyone have any thoughts? Does the above even make sense? Anyway,  
thanks again for reading this far.

Cheers
Dan







-------------- next part --------------
An HTML attachment was scrubbed...
URL: http://mailman.ecs.soton.ac.uk/pipermail/osx-users/attachments/20090323/6020e788/attachment.html 


More information about the Osx-users mailing list