|
#1
| |||
| |||
| 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! |
|
#2
| ||||
| ||||
| 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. |
|
#3
| |||
| |||
| Thanks much, I really appreciate it! |
|
#4
| |||
| |||
| To be pedantic: Code: id foo; Code: foo=nil; 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] 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." |
|
#5
| |||
| |||
| Whoa! Now I REALY understand it! Thanks a lot for you help, I greatly appreciate it! Happy Coding!! -Ray |
|
#6
| |||
| |||
| 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 Code: NSArray *myArray = nil; int objectCount = [myArray count]; // objectCount is now undefined |
|
#7
| |||
| |||
| Actually, I just found some code where I did: Code: int num = [some_random_object count]; F-bacher
__________________ James Tiberius Kirk : "Spock, the women are your planet are logical. No other planet in the universe can make that claim." |
|
#8
| |||
| |||
| Quote:
BTW, I do not know how is nil actually implemented. |
![]() |
| Thread Tools | |
|
|
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 |