Re: SOLVED Question for the Java Gurus
Re: SOLVED Question for the Java Gurus
- Subject: Re: SOLVED Question for the Java Gurus
- From: Nathan Hampton <email@hidden>
- Date: Thu, 22 Apr 2004 23:27:35 -0500
KVC is great if all you need are the data elements of your object, but
what if you need a particular method? Or, perhaps, if you needed
execute aClass.aMethod() or anotherClass.anotherMethod(), depending on
whether the object is an instance of aClass or anotherClass? Has
anyone tried Jake's suggestion (a slightly modified version appears
below), which seems like it might address this situation? Does it
work? I realize, of course, that what I'm proposing is a completely
bizarre, highly improbable, extremely esoteric situation that nobody
would ever come across in real life, but I never thought of beer
goggles, either, which tells you how much I know. :)
--NCH
PS: My rendition of Jake's suggestion:
public getOtherObjects(String entity, NSArray objects)
{
for (int i=0; i < objects.count(); i++)
{
Class.forName(entity) tempOb =
(Class.forName(entity))array.objectAtIndex(i);
}
}
PPS: This is, naturally, all idle speculation on my part, and if I ever
get around to finishing the project I'm working on, I might take an
afternoon to sort this out as an intellectual exercise, so don't worry
about it if, like myself, you've managed to spend all your time for
idle speculation before finishing your actual work.
On 22 Apr 2004, at 6:41 PM, Owen McKerrow wrote:
Hi All,
Thanks for the suggestions, its the first time Ive tried to go to this
level of obstraction, so Im discovering all these fun new things EOF
can/can't do for me.
Hmmm. So instead of casting to a Object, I should cast it to an
EOCustomObject, (which makes much more sense, and Im not sure why I
didn't think about that yesterday). And then use the valueForKey
methods. So I guess I could write the function as :
public static NSMutableArray stripJoin(NSArray objects, String
relationship) throws Exception
{
int i;
NSMutableArray wantedObjects = new NSMutableArray();
for (i=0;i < objects.count();i++) {
EOCustomObject temp = (EOCustomObject )objects.objectAtIndex(i);
wantedObjects.addObject(temp.valueForKey(relationship));
}
return wantedObjects;
}
Is that correct ?
Looking at what Sam Barnum suggested :
NSArray arrayOfEats; // assume this exists
NSArray arrayOfFrogs = (NSArray)arrayOfEats.valueForKey("frog");
NSArray arrayOfFoods = (NSArray)arrayOfEats.valueForKey("food");
How does this work ?
If you have an array of Eats, will calling arrayOfFrogs =
(NSArray)arrayOfEats.valueForKey("frog"); Return an array of frogs
where each instance in the arrayOfFrogs array belongs to a specific
Eats object in the eats array ?
i.e. Eats Array = B1,B2,B3,B5
arrayOfFoods would then equal :
B1.eats(),B2.eats(),B3.eats(),B5.eats() ?
Is that what you mean Sam ?
Owen
On 23/04/2004, at 1:11 AM, B. Olav Anderson wrote:
Hi,
Isn't this the same as
objects.valueForKey(relationship);
or
objects.valueForKeyPath(relationship);
?
On Wednesday, April 21, 2004, at 10:06 PM, Owen McKerrow wrote:
Yeah sorry, re-read the email and yes I didn't explain myself very
well. The function was just an example really, just to put the
Object tempOb = (entity)array.objectAtIndex(0); line in some form of
context.
The question was basically, given the name of a class as a string,
is there anyway to use that String for casting an object to the
class type of class who's name is the contents of the string.
In the end I did it another way :
/**This class is deisgned to take a NSArray of objects from a join
table between two entities and return a NSArray of the objects from
one of the entities.
That is if you have 2 tables 'Frog' and 'Food" with the join table
'Eats' between them, you can pass this class the Frogs NSArray of
Eats objects and ask for an NSArray of Food items that match the
given relationship.
It only every really needs to be used when there is something in the
join table that doesn't allow it to be flattened.*/
/**
Given an Array of join table objects and a relationship name, it
will seek to return an array of objects from the other side of that
relationship
@param objects The array of objects you wish to strip
@param relationship The relationship that you wish to get an array
of objects from
*/
public static NSMutableArray stripJoin(NSArray objects, String
relationship) throws Exception
{
int i;
NSMutableArray wantedObjects = new NSMutableArray();
for (i=0;i < objects.count();i++) {
Object temp = objects.objectAtIndex(i);
Method tempMethod = temp.getClass().getMethod(relationship,null);
wantedObjects.addObject(tempMethod.invoke(temp,null));
}
return wantedObjects;
}
Owen
On 22/04/2004, at 2:17 PM, Art Isbell wrote:
On Apr 21, 2004, at 5:48 PM, Owen McKerrow wrote:
How can I make the following code work if entity is an instance of
String passed in as an argument to the function. i.e. Can I cast
to a specific Class if all I have is the name of the Class as a
String ? If not is there another way I can do this ?
public getOtherObjects(String entity, NSArray objects) {
for(int i=0;i<objects.count();i++) {
Object tempOb = (entity)array.objectAtIndex(0);
}
}
Not sure what you're trying to do since the above function really
does nothing. If objects is an array of enterprise objects, then
you can assign an array element to a variable of type
EOEnterpriseObject:
EOEnterpriseObject tempOb =
(EOEnterpriseObject)objects.objectAtIndex(0);
Aloha,
Art
_______________________________________________
webobjects-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/webobjects-dev
Do not post admin requests to the list. They will be ignored.
'The test of a first-rate intelligence is the ability to hold two
opposed ideas in the mind at the same time and still be able to
function.'
-F.Scott Fitzgerald,
_______________________________________________
webobjects-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/webobjects-dev
Do not post admin requests to the list. They will be ignored.
'The test of a first-rate intelligence is the ability to hold two
opposed ideas in the mind at the same time and still be able to
function.'
-F.Scott Fitzgerald,
_______________________________________________
webobjects-dev mailing list | email@hidden
Help/Unsubscribe/Archives:
http://www.lists.apple.com/mailman/listinfo/webobjects-dev
Do not post admin requests to the list. They will be ignored.
_______________________________________________
webobjects-dev mailing list | email@hidden
Help/Unsubscribe/Archives: http://www.lists.apple.com/mailman/listinfo/webobjects-dev
Do not post admin requests to the list. They will be ignored.