Readonly, non-mutable, public and readwrite, mutable, private @property: more information?

I want to show NSArraymy user (and I want them to only read it), but in my class I want to use NSMutableArray.

I tried the following code and it does not raise any warnings:

// In the .h
@interface MyClass : NSObject <NSApplicationDelegate>

@property (nonatomic, readonly) NSArray * test ;

@end

and

// In the .m
@interface MyClass ()

@property (nonatomic, strong, readwrite) NSMutableArray * test ;

@end


@implementation MyClass

- (id)init
{
    self = [super init];
    if (self)
    {
        self.test = [[NSMutableArray alloc] init] ;
    }
    return self;
}

@end

But if I try to access @property testfrom my class, I can use the method addObject:. So, I assume that the antecedent is impossible.

Why are there no warnings?

+4
source share
2 answers

, . , . . : self -init::

// In the .h
@interface MyClass : NSObject <NSApplicationDelegate>

- (NSArray *)test;

@end

// In the .m
@interface MyClass ()

@property (nonatomic, strong) NSMutableArray *aTest;

@end


@implementation MyClass

- (id)init
{
    self = [super init];
    if (self)
    {
        _aTest = [[NSMutableArray alloc] init] ;
    }
    return self;
}

- (NSArray *)test
{
    return [self.aTest copy];
}

@end
+2

@property - , getter/setter. readonly .h getter , , .m, .

readwrite (. ), readwrite - @property , . readwrite, .m, , ​​ .h.

0

Source: https://habr.com/ru/post/1526560/


All Articles