2

I am having the following code:

@interface Room : NSObject
{
@protected
    NSMutableDictionary* mCustomProperties;
}

@property (readonly, copy) NSDictionary* CustomProperties;

@end



@interface MutableRoom : Room
{
}

@property (readwrite, retain) NSMutableDictionary* CustomProperties;

@end

These properties are then implemented later by accessing mCustomProperties. I know, that it is not allowed to redeclare the memory management attribute or the data type of a property in a subclass to differ from the ones in the base class. However, I want to achieve the following: - Give only readonly access to the dictionary in the immutable base class. As the member is a NSMutableDictionary, just returning it as a retained instance of NSDictionary would mean, that it simple cast would make it mutable again, even unintended, when the user of the property stores the access variable somewhere as an NSObject instance and then recovers the original type of it back later. Therefor I want to return the mutable dictionary by immutable copy in the readonly property. - Give full readwrite access to the dictionary in the mutable sub class. As you should not only be able to store another dictionary there, but also to just change the content of the existing one, I would like the property in the subclass not only to be readwrite, but also to be retained, instead of copied, so that the original values will be accessed, and to access it as an NSMutableDictionary, not as a NSDictionary, so that one can add, remove or change entries of the dictionary through the property.

Is there any more elegant way than suppressing the warning or using two differently named properties?

1 Answer 1

1

when declaring a mutable/immutable pair of a class cluster, i often find it simplest to not inherit from another, but to use an inner class for the implementation, like so:

// Room.h
@interface Room : NSObject
// ...
@end

@interface MutableRoom : Room
// ...
@end

// MONRoom.h
@class RoomImp;

@interface MONRoom : Room
{
@private
    RoomImp * imp;
}

@property (readonly, copy) NSDictionary* CustomProperties;

@end

@interface MONMutableRoom : MutableRoom
{
@private
    RoomImp * imp;
}

@property (readwrite, retain) NSMutableDictionary* CustomProperties;

@end

// RoomImp.h
@interface RoomImp : NSObject
{
@private
    NSMutableDictionary* mCustomProperties;
}

// ...

@end
5
  • Yeah, implementation looks a bit more complex now of course, but this way I could actually achieve the intended interface :)
    – Kaiserludi
    Nov 23, 2011 at 13:18
  • Well, thought a bit more about your approach: That way I can only use that property on MON(-Mutable-)Room, not on (Mutable-)Room, can I? But of course I have to use (Mutable-)Room in all interfaces, so that one can pass the mutable version, where the immutable is specified as parameter type. That would mean, that one would have to cast down all the(Mutable-)Room-instances to a MON(-Mutable-)Room, everywhere, where a access of the property is needed, otherwise one won't be able to use Dot-Syntax on them. So it seems, this isn't really a suitable solution.
    – Kaiserludi
    Nov 23, 2011 at 18:24
  • @Kaiserludi generally, it works fine - you use the immutable/immutable variants to provide the specialized copy semantics - basically, to provide the public methods and any additional semantics when using RoomImp. it's also risky to expose mutable members publicly. instead, you may see something along the lines of setProperties: or setProperty:value:.
    – justin
    Nov 23, 2011 at 19:22
  • "it's also risky to expose mutable members publicly." Well, that depends on the usage scenario. And where is the difference in exposure, between having a manual setter method and a property with write-access? Imho thats just a syntactic difference. Sending around RoomImp-instanceces, would mean, that you would always have to do something like this: [MONRoom roomWithRoomImp:myRoomImp].CustomProperties, to access the property. That does not seem very straightforward to me.
    – Kaiserludi
    Nov 24, 2011 at 12:03
  • Typical usage in this design: The RoomImp provides the shared implementation. the Room and MutableRoom provide the public interface visible to clients. RoomImp is an internal class used to provide the shared implementation, and the Room and MutableRoom abstract that. Clients which use the Room do not (generally) create, mutate, or know of the RoomImp because it is abstracted, and it is visible or accessible to clients. If you look at the system frameworks, you will also see that exposing mutable members is very unusual - it's rare to see a class return NSMutableSomething.
    – justin
    Nov 25, 2011 at 6:50

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Not the answer you're looking for? Browse other questions tagged or ask your own question.