• 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: Two odd newbie questions ;-)
[Date Prev][Date Next][Thread Prev][Thread Next][Date Index][Thread Index]

Re: Two odd newbie questions ;-)


  • Subject: Re: Two odd newbie questions ;-)
  • From: Ondra Cada <email@hidden>
  • Date: Thu, 12 May 2005 23:12:51 +0200

Felix,

On 12.5.2005, at 20:33, Felix Schwarz wrote:

I'm (pretty) new to Objective-C programming. One thing I keep wondering about, is whether I can safely write e.g.

myFloat = [[valueLimits objectForKey:myKey] floatValue];

Never.

or should rather write:

    if (myNsFloat = [valueLimits objectForKey:myKey])
    {
        myFloat = [myNsFloat floatValue];
    }

Definitely.

The same goes with

    [new retain];
    [old release];
    old = new;

This one is completely all right.

    if (new)
    {
        [new retain];
    }

    if (old)
    {
        [old release];
    }

Superfluous.

Is it legal to perform methods on (or send messages to) "null"

No, null is a Java thing, and in Java (triple alas) you cannot "send message" (in Javaspeak, "call method of") a null.


or "nil" objects?

Completely. In Objective C, you *always* can send safely *any* message to nil. What happens is an empty operation with a nil result.


The nil result means that, by definition, if the message returns an object, you can directly and safely use it:

[[[obj window] title] componentsSeparatedByString:@" "]

works all right regardless obj, [obj window], or [... title] happen to be nil.

Since objects are just pointers, it is completely safe to use a pointer result, like

char *ptr=[str cString]; // yeah, I know, deprecated

all right if str is nil, prt will be NULL.

Since in practice pointers and ints are from the CPU point of view the same, it *in practice* is safe with ints, longs, BOOLs, enums, and whatever alike:

if ([str length]==0) // this code done for str==nil AND for str==@""

Nevertheless, this usage is very slightly dangerous: although the probability is *very* low, still it is possible a future or different (GNUStep) compiler may make this *not* work correctly.

Finally, with floats and structs, you CANNOT use the result (the sending is all right):

float f=[s floatValue]; // for s==nil the result is essentially RANDOM!!
NSPoint pt=[event locationInWindow]; // for event==nil, the result is essentially RANDOM!!


Is there a guaranteed result (thinking of above myFloat example - will it be null in case the dictionary does not contain the key or have an undefined value?)?

As said: with objects and other pointers, yes (nil/NULL). With integer values, in practice yes (0), though it is slightly unclean. With others *nope*.
---
Ondra Čada
OCSoftware: email@hidden http://www.ocs.cz
private email@hidden http://www.ocs.cz/oc



_______________________________________________ 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: 
 >Two odd newbie questions ;-) (From: Felix Schwarz <email@hidden>)

  • Prev by Date: Re: Getting disk mount notification with Foundation tools
  • Next by Date: Re: [OT] How does one report bugs to Apple about their applications?
  • Previous by thread: Re: Two odd newbie questions ;-)
  • Next by thread: Using Core-Image in GUI
  • Index(es):
    • Date
    • Thread