Re: Why add a nil item to an NSArray ?
Re: Why add a nil item to an NSArray ?
- Subject: Re: Why add a nil item to an NSArray ?
- From: Clark Cox <email@hidden>
- Date: Mon, 27 Jun 2005 19:19:18 -0400
On 6/27/05, Mark Munz (DevList) <email@hidden> wrote:
> It is non-intuitive. Is this maybe something left over from the past?
>
> The variable argument method already has process a variable list of
> arguments already and can easily determine when the end is reached.
No, it cannot automatically do that; any such function taking a
variable number of arguments needs more information.
>
> Other methods like [NSString stringWithFormat:(NSString*)format,...]
> work as you would expect.
stringWithFormat (like printf) knows when to stop looking for
parameters by counting the number of format specifiers in the first
parameters.
You basically have the following options when writing such a function:
1) Agree on the number of arguments ahead of time (but then there is
no point in making it a varardic function)
2) Have the first parameter tell you how many items follow (with a
format string like printf/stringWithFormat:, or with an explicit
count)
3) Have some sentinel value marking the end of the list (like
arrayWithObjects:).
> But NSArray and NSDictionary are very unhappy campers if you don't
> end your definitions with a nil.
For the same reason that NSString will be an unhappy camper in the following:
[NSString stringWithFormat: @"Expects two parameters: %d %@", 5];
//But I only provide one
--
Clark S. Cox III
email@hidden
http://www.livejournal.com/users/clarkcox3/
http://homepage.mac.com/clarkcox3/
_______________________________________________
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