How to declare an immutable property backed by a mutable type?

前端 未结 3 1189
一向
一向 2020-12-11 00:29

I’d like to declare a public immutable property:

@interface Foo
@property(strong, readonly) NSSet *items;
@end

…backed with a mutable type

3条回答
  •  轮回少年
    2020-12-11 01:18

    The easiest solution is

    @interface Foo {
    @private
        NSMutableSet* _items;
    }
    
    @property (readonly) NSSet* items;
    

    and then just

    @synthesize items = _items;
    

    inside your implementation file. Then you can access the mutable set through _items but the public interface will be an immutable set.

提交回复
热议问题