Should I throw when returning id from objective-c method or not?

For the Objective-C guru:

Suppose I have a simple way:

-(id)getValue{ return [NSNumber numberWithDouble:5.0]; }

Now suppose that in some other method, I call the getValue method (id) as follows:

NSNumber* myValue = [self getValue];

or what if I call it this:

NSNumber* myValue = (NSNumber*)[self getValue];

Question: Obviously, these lines are equivalent, but one of them uses an explicit cast. So what is the right or best way to do this. It seems to me that the cast is not needed, because when it is placed in the myValue index, it will be safe in this case in any case (this is what I want), so casting is basically pointless.

, , : (NSNumber *) getValue, , , , NSDictionary id, : objectForKey, NSObject . , getValue NSNumber. , , , .

,

-

+3
1

- . ( .) , , :

((Foo *)[myArray objectAtIndex:0]).bar;

, .

, id, , , . " ", id ; , , Objective-C. . * (, , ). , , , .

, getX. Cocoa; , "" - , . . -[NSArray getObjects:range:] .


* , , , , , .

+5

All Articles