image
image

Go Back   macosx.com > Design, Media, Programming & Scripting > Software Programming & Web Scripting

Reply
 
Thread Tools
  #1  
Old June 21st, 2002, 02:31 AM
ECE Student
 
Join Date: Mar 2001
Location: University of Arizona
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
macxonly is on a distinguished road
Question Sending Messages to nil....

Help! Newbie warning...

I am currently reading Aaron Hillegass' book, and am confused on page 42.

The code reads:

id foo;
foo=nil;
[foo count];


Can somebody explain what each line of the code does, and possibly explain why I "should be happy" about this. (I understand the Java code before this example, so no help needed there) Thanks all it will be greatly appreciated!
Reply With Quote
  #2  
Old June 21st, 2002, 12:15 PM
zots's Avatar
Re-member
 
Join Date: Jun 2001
Location: Ft Lauderdale
Posts: 94
Thanks: 0
Thanked 0 Times in 0 Posts
zots is on a distinguished road
id foo;
declares an object named foo.

foo=nil;
sets foo's value to nil/null.

[foo count];
returns foo's count. should be zero. if you had an array, the count method would be useful to get the number of elements in your array.
__________________
Plaisir d'amour ne dure qu'un moment,
Chagrin d'amour dure toute la vie.
Reply With Quote
  #3  
Old June 21st, 2002, 02:02 PM
ECE Student
 
Join Date: Mar 2001
Location: University of Arizona
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
macxonly is on a distinguished road
Talking Thanks much

Thanks much, I really appreciate it!
Reply With Quote
  #4  
Old June 21st, 2002, 06:44 PM
Registered User
 
Join Date: Oct 2000
Location: U of I @ Champaign
Posts: 208
Thanks: 0
Thanked 0 Times in 0 Posts
Ghoser777 is on a distinguished road
To be pedantic:

Code:
id foo;
Declares a variable of type id, which is an unspecified Obj-C type, much like void * in C. So you can have foo reference any Obj-C object you create.

Code:
foo=nil;
foo now points to nothing. So if you try to send a message to it, where does the message go? To nothingness.

If you try to send a message to foo before pointing it to something (even nothingness!), then your app will die and crash painfully.

Code:
[foo count]
Obj-C allows you to send messages to objects that may or may not respond to them. Java, on the othere hand, cries like a baby because it's strongly typed. Because NSDictionary and NSArray have methods called count, the Obj-C runtime knows that sending the message count to an object (not that we know which one) is okay. It's up to the user to make sure that foo points to an object that actually implements that function. Errors will be reported at runtime instead of at compile time (like in Java). If foo was actually declared to be of some type other than one that implements count, then you would receive an error at compile time.

In Java, you can't call functions off of nothing (which kind of makes sense), but in Obj-C you can get away with it (messaging to nothing). I would actually prefer to know if I'm sending messages to nil, but I'll live. In Java, your app would probably crash or begin to behave strangely, but in Obj-C it's a normal thing.

Just for the record,
F-bacher
__________________
James Tiberius Kirk : "Spock,
the women are your planet are
logical. No other planet in the
universe can make that claim."
Reply With Quote
  #5  
Old June 22nd, 2002, 06:09 PM
ECE Student
 
Join Date: Mar 2001
Location: University of Arizona
Posts: 24
Thanks: 0
Thanked 0 Times in 0 Posts
macxonly is on a distinguished road
Whoa!

Now I REALY understand it! Thanks a lot for you help, I greatly appreciate it! Happy Coding!!

-Ray
Reply With Quote
  #6  
Old June 24th, 2002, 05:47 AM
Spackle King Of Helsinki
 
Join Date: Feb 2002
Location: Tempe, AZ
Posts: 45
Thanks: 0
Thanked 0 Times in 0 Posts
smeger is on a distinguished road
One more quick note: According to Apple's Objective-C language reference, you can safely send a message to a nil object provided that the message returns an object. The returned object will always be nil. However, if the return value is not an object, the return value is undefined. So, you could safely do something like
Code:
NSArray *myArray = nil;
id lastObject = [myArray lastObject];
// lastObject is now set to nil
but you could not safely do something like
Code:
NSArray *myArray = nil;
int objectCount = [myArray count];
// objectCount is now undefined
__________________
Tired of Aqua? Ditch it and create your dream theme with ThemePark.
Reply With Quote
  #7  
Old June 26th, 2002, 01:35 PM
Registered User
 
Join Date: Oct 2000
Location: U of I @ Champaign
Posts: 208
Thanks: 0
Thanked 0 Times in 0 Posts
Ghoser777 is on a distinguished road
Actually, I just found some code where I did:

Code:
     int num = [some_random_object count];
And some_random_object was nil! I think it just converted nil to 0.

F-bacher
__________________
James Tiberius Kirk : "Spock,
the women are your planet are
logical. No other planet in the
universe can make that claim."
Reply With Quote
  #8  
Old June 27th, 2002, 01:01 PM
Unperson Spotter
 
Join Date: Mar 2001
Posts: 275
Thanks: 0
Thanked 0 Times in 0 Posts
ladavacm is on a distinguished road
Quote:
Originally posted by Ghoser777
Actually, I just found some code where I did:

Code:
     int num = [some_random_object count];
And some_random_object was nil! I think it just converted nil to 0.

F-bacher
Probably because nil is implemented using a bit pattern of all zeros (which makes sense, since testing for value of zero is inherently faster than testing for any other specific value)

BTW, I do not know how is nil actually implemented.
Reply With Quote
Reply

Thread Tools

Posting Rules
You may not post new threads
You may not post replies
You may not post attachments
You may not edit your posts

BB code is On
Smilies are On
[IMG] code is On
HTML code is Off
Trackbacks are Off
Pingbacks are Off
Refbacks are Off
Forum Jump

Similar Threads
Thread Thread Starter Forum Replies Last Post
sending messages avatar_000 Unix & X11 7 May 8th, 2003 09:01 AM
Mail.app woes: reloads the same messages again and again... Giaguara Mac OS X System & Mac Software 2 May 7th, 2003 04:38 PM
Problems sending messages with sendmail Fabrizio Mac OS X System & Mac Software 7 December 10th, 2002 11:48 AM
sending messages via AppleTalk Zeus Mac OS X System & Mac Software 0 June 1st, 2002 06:21 AM
Importing messages into Entourage X newyorkmidget Mac OS X System & Mac Software 0 April 12th, 2002 10:26 PM


All times are GMT -5. The time now is 05:41 AM.


Mac Support® Version 3.7.2
Copyright ©2000 - 2009, Jelsoft Enterprises Ltd.
Search Engine Friendly URLs by vBSEO 3.1.0
Copyright 2000-2008 DigitalCrowd, Inc.