On 3-Jun-08, at 12:56 PM, Karl Moskowski wrote:
I have a few methods that return NSData objects, but the objects are
created and manipulated as NSMutableData, and then copied to an
immutable version along these lines:
NSMutableData *myData = [NSMutableData data];
:
:
return [NSData dataWithData:myData];
Would it be sufficient to cast, like this?
:
return (NSData *)myData;
Does this generalize to other non-collection classes, e.g., NSString?
Hello -
It depends if you actually want to return an immutable object or not.
When you call a method, the return type isn't a guarantee that the
returned value will be a member of the specific class shown, but
rather a member of or a subclass of the class shown. You can return
that NSMutableData without casting it from a method which says it
returns NSData and it's still technically valid.
For example, in these three classes only the first returns an NSData
object, the other two return NSMutableData objects
- (NSData *) returnImmutableData {
NSMutableData *someData = [NSMutableData data];
return [NSData dataWithData:someData];
}
- (NSData *) returnSomeData {
NSMutableData *someData = [NSMutableData data];
return (NSData *)someData;
}
- (NSData *) returnSomeOtherData {
NSMutableData *someOtherData = [NSMutableData data];
return someOtherData;
}
In general, I'd say it's best to return what you're actually claiming
to return.
-Bob Warwick
[EMAIL PROTECTED]
_______________________________________________
Cocoa-dev mailing list (Cocoa-dev@lists.apple.com)
Please do not post admin requests or moderator comments to the list.
Contact the moderators at cocoa-dev-admins(at)lists.apple.com
Help/Unsubscribe/Update your Subscription:
http://lists.apple.com/mailman/options/cocoa-dev/archive%40mail-archive.com
This email sent to [EMAIL PROTECTED]