• Open Menu Close Menu
  • Apple
  • Shopping Bag
  • Apple
  • Mac
  • iPad
  • iPhone
  • Watch
  • TV
  • Music
  • Support
  • Search apple.com
  • Shopping Bag

Lists

Open Menu Close Menu
  • Terms and Conditions
  • Lists hosted on this site
  • Email the Postmaster
  • Tips for posting to public mailing lists
Re: Why add a nil item to an NSArray ?
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Why add a nil item to an NSArray ?


  • Subject: Re: Why add a nil item to an NSArray ?
  • From: Bob Ippolito <email@hidden>
  • Date: Mon, 27 Jun 2005 18:37:08 -0400

This is just how C and Objective-C variable argument lists must work. The argument count is not part of the Mach-O calling convention so the number of arguments to process must either be sent explicitly or determined by some other means. In the NSString example, the string format determines exactly how many items to expect. With the NSArray and NSDictionary methods, the only way to know how many items were passed is to use a sentinel value to denote the end of the argument list, which is nil. It's not legacy, it MUST be done this way.

-bob

On Jun 27, 2005, at 6:20 PM, Mark Munz (DevList) 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.

Other methods like [NSString stringWithFormat:(NSString*)format,...]
work as you would expect.

But NSArray and NSDictionary are very unhappy campers if you don't end your definitions with a nil.

Mark Munz

On Jun 27, 2005, at 2:37 PM, SA Dev wrote:



That's how the 'method' 'knows' that you've given it the last item in the list so it can know for sure when it reaches the end of the arguments for the method.




On Jun 27, 2005, at 3:08 PM, Martin Ottenwaelter wrote:



Hi,

Why doesn't that works :
[NSArray initWithObjects:@"First",@"Second"];

Why do I have to add that nil item for the NSArray to be ok ?
[NSArray initWithObjects:@"First",@"Second",nil];

Thanks.
--
Martin

_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
40silentalcove.net


This email sent to email@hidden




_______________________________________________
Do not post admin requests to the list. They will be ignored.
Cocoa-dev mailing list (email@hidden)
Help/Unsubscribe/Update your Subscription:
40unmarked.com


This email sent to email@hidden





_______________________________________________ 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


_______________________________________________ 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
References: 
 >Why add a nil item to an NSArray ? (From: Martin Ottenwaelter <email@hidden>)
 >Re: Why add a nil item to an NSArray ? (From: SA Dev <email@hidden>)
 >Re: Why add a nil item to an NSArray ? (From: "Mark Munz (DevList)" <email@hidden>)

  • Prev by Date: Re: Why add a nil item to an NSArray ?
  • Next by Date: Predicate on two values
  • Previous by thread: Re: Why add a nil item to an NSArray ?
  • Next by thread: Re: Why add a nil item to an NSArray ?
  • Index(es):
    • Date
    • Thread