Re: Eval() ?
Re: Eval() ?
- Subject: Re: Eval() ?
- From: "Thierry Passeron" <email@hidden>
- Date: Fri, 3 Mar 2006 12:47:40 +0100
2006/3/3, Andrew Farmer <email@hidden>:
> On 02 Mar 06, at 19:57, Brett Powley wrote:
> > On 03/03/2006, at 1:00 PM, Andrew Farmer wrote:
> >> Cocoa is a superset of C, which is a compiled language; as such
> >> there's no direct way to compile and run code at runtime. However,
> >> there are ways to construct method calls, so long as that's all
> >> you're doing. It's difficult, however; you may want to try
> >> explaining what you're after to see if there's some more
> >> straightforward way of doing what you're looking for.
> >
> > This isn't really correct.
> >
> > Objective-C is a superset of C with a dynamic object model, so that
> > you *can* determine properties about an object at run-time and
> > invoke methods on it. Cocoa is a library/framework written in
> > Objective-C.
> >
> > What you need to look at is the performSelector: methods of NSObject.
> >
> > For example, you could do:
> >
> > id object = [someObject performSelector:sel_getUid("method1")
> > withObject:aparameter withObject:antOtherparameter];
> >
> > For method calls that you can't do using performSelector (i.e.
> > those with arguments or return values that aren't NSObjects), you
> > can use NSInvocation.
>
> Point is, there's no way you can expect to run something like
>
> char *eval_str = "[Object doSomething:someFunctionCall(etc etc)]";
> eval(eval_str);
>
> and expect it to work. Reflection is a powerful technique, but it
> only goes so far.
>
> My point stands that it'd probably help - a LOT - to know what the OP
> is really trying to do, and it might make sense to look for an
> alternate way of going about it.
>
>
>
Well I just wanted to know if there was a way to do that kind of
eval() in a cocoa/ObjC code.
Anyhow, for my problem, I have already figured out how to deal with it.
I have a formatted NSString, an NSArray of parameters (with variable
number of elements).
And I want to output the result (NSString *) of the combination of
format and parameters.
it would have been very easy (after controlling the number/type of
parameters of course) to eval such a NSString with something like:
//NSArray *array (the array of NSString parameters)
//NSString *format ( something like @"this %@ a sample %@")
NSMutableString * parameters=[[NSMutableString alloc]init];
for(i=0;i<[array count];i++){
[parameters appendString:@",@\""];
[parameters appendString:[array objectAtIndex:i]];
[parameters appendString:@"\" "];
}
eval_str=[NSMutableString alloc] initWithFormat:@"[[NSString
alloc]initWithFormat:format %@",parameters];
NSString * result=(NSString *)NSEval(eval_str);
BUT ... I can do it an other way.
I will replace each %@ of the formatted string with the element
associated in the array.
It may be even easier.
Best regards,
Thierry.
_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
This email sent to email@hidden