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

前端 未结 3 1178
一向
一向 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:21

    You have to declare the property in the public interface as readonly in order the compiler not to complain. Like this:

    Word.h

    #import 
    
    @interface Word : NSObject
    @property (nonatomic, strong, readonly) NSArray *tags;
    @end
    

    Word.m

    #import "Word.h"
    
    @interface Word()
    @property (nonatomic, strong) NSMutableArray *tags;
    @end
    
    @implementation Word
    @end
    

提交回复
热议问题