| |
 |
|
| Computers Forum Index » Computer Languages (Objective-C) » Studies in Obj-C : disappointment ? |
|
Page 1 of 1 |
|
| Author |
Message |
| Francis Derive |
Posted: Mon Aug 30, 2004 10:39 pm |
|
|
|
Guest
|
Bonjour !
I am starting with Obj-C and - somehow - i know where I want to go,
but what about "Dynamic Binding" ?
I could have such a class method
+ (NSArray *) list: (id) arg, ... {
return [NSArray arrayWithObjects: arg ];
}
which does return a list of its arguments: [NSArray list:@"a", @"b",
@"c", nil ].
What if "Dynamic Binding" with Obj-C ?
SEL theSelector = @selector(list:);
[NSArray performSelector:theSelector withObject:@"a"
withObject:@"b" .... ???
How to face - here - the variable arguments number ? How can we be
limited to 2 occurrences of "withObject:" ? Could it be so
disapointing ?
Cheers - anyway. |
|
|
| Back to top |
|
|
|
| Peter Ammon |
Posted: Thu Sep 16, 2004 9:02 am |
|
|
|
Guest
|
Francis Derive wrote:
Quote: Bonjour !
I am starting with Obj-C and - somehow - i know where I want to go,
but what about "Dynamic Binding" ?
I could have such a class method
+ (NSArray *) list: (id) arg, ... {
return [NSArray arrayWithObjects: arg ];
}
which does return a list of its arguments: [NSArray list:@"a", @"b",
@"c", nil ].
Does this actually work? Even if it does, don't do it. Use stdarg like
it was meant to be used.
+ (NSArray*)list:(id)arg, ... {
if (! arg) return [NSArray array];
va_list argp;
unsigned arg_count = 1;
va_start(argp, arg);
while ((obj = va_arg(argp, id))) {
count++;
}
va_end(argp);
NSMutableArray* result = [NSMutableArray arrayWithCapacity:count];
[result addObject:arg];
va_start(argp, arg);
while ((obj = va_arg(argp, id))) {
[result addObject:obj];
}
va_end(argp);
return result;
}
Quote:
What if "Dynamic Binding" with Obj-C ?
SEL theSelector = @selector(list:);
[NSArray performSelector:theSelector withObject:@"a"
withObject:@"b" .... ???
How to face - here - the variable arguments number ?
You don't. The variadic function mechanism isn't rich enough to do the
sort of reflection that Objective-C needs. The best you can do is a
chain of if-then statements. (Note that NSInvocation doesn't do
variadic functions).
Quote: How can we be
limited to 2 occurrences of "withObject:" ? Could it be so
disapointing ?
If you want more, you can easily add more, with categories.
--
Pull out a splinter to reply. |
|
|
| Back to top |
|
|
|
|
|
All times are GMT
The time now is Sun Nov 08, 2009 10:32 am
|
|