Re: Casting to a superclass
Re: Casting to a superclass
- Subject: Re: Casting to a superclass
- From: Raphael Sebbe <email@hidden>
- Date: Mon, 16 Jul 2001 17:08:04 +0200
On Monday, July 16, 2001, at 04:50 PM, Rob Rix wrote:
Hello, all.
I'm writing a utility class, which will have an NSMutableSet as an
ivar. I want users of the class to be able to access the set, but as an
immutable object.
So, can I typecast the NSMutableSet to NSSet, or should I just return
[NSSet setWithSet:someSet];?
You can actually do both, but casting won't change the internal
representation of the set and it will thus keep its mutability. If you
really want to preserve it, use the later approach. Also note that users
of your API shouldn't try to add objects to the returned set if it's
said to be immutable. For this reason, I believe the former approach is
widely accepted and used...
Raphael