Re: Empty NSStrings, nil NSStrings
Re: Empty NSStrings, nil NSStrings
- Subject: Re: Empty NSStrings, nil NSStrings
- From: j o a r <email@hidden>
- Date: Sun, 16 Apr 2006 13:59:01 +0200
On 16 apr 2006, at 13.01, Paolo Bertani wrote:
From a "Cocoa" point of view a nil NSString and an empty NSString
(a valid NSString instance containing 0 characters) can/should be
considered interchangeable?
They're not at all interchangeable. A nil value is not a string at
all. Think of nil as another word for "nothing" - It's not an object
at all. An empty string is a string that doesn't have any characters,
but it's still an object, and it can respond to methods (for example
[str length]).
Or should I check every NSString and, if nil, turn it into an empty
NSString before working with it (messaging or passing as parameter)?
1) About sending messages to nil
You can't send messages to nil, well, you can - but they will really
be ignored by the runtime. It doesn't make sense to send messages to
"nothing". Sending a message to an object is like talking to it. If
there is no object to talk to, it's like you're talking to yourself -
not much use in doing that.
2) About sending a nil value as a parameter to other methods
You can send nil instead of objects to methods, but how they will
respond differs between methods. In some cases it has a meaning and
it will work OK, in other cases it is treated as an error, and you
might get an exception. You have to check the documentation for each
case.
j o a r
Attachment:
smime.p7s
Description: S/MIME cryptographic signature
_______________________________________________
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