2014-12-02 3 views
0

Я использую изменяемый массив. Я не уверен, что это правильный способ управления массивом. Я попытался изучить основы управления памятью, но мне было слишком сложно понять. Что я делаюПравильный способ управления массивом

объявить массив на границе

@interface myVC() 
@property (nonatomic,strong) NSMutableArray *substrings; // if I use weak attribute here,does it change anything? 
@end 


-(void)myMethod{ 

    // initializing the array 
    _substrings=[[NSMutableArray alloc]init]; 

    //storing some data into it 
    [_substrings addObject:@"hello"]; 

    //do something with the data in that array 
     //call another method which gets the data from this same array and do some operations there 
      [self method2];-----> // I access data from the array like, x=[_substrings objectatindex:0]; 

    //finally, remove the items in the array 
     [_substrings removeObject:@"hello"]; 

     //and again start the process as mentioned here 


    } 

Это то, что я имею в виду сделать. Является ли это правильным способом объявления и доступа к массиву?

ответ

1

В целом это сработает, однако я бы рекомендовал получить доступ к этому массиву с помощью свойства getter/setter. Таким образом, если вам когда-либо понадобится создать пользовательский getter/setter, вам не нужно будет реорганизовывать весь ваш код.

@interface myVC() 
@property (nonatomic, strong) NSMutableArray *substrings; 
@end 


-(void)myMethod{ 

    // initializing the array 
    _substrings=[[NSMutableArray alloc]init]; 

    //storing some data into it 
    [self.substrings addObject:@"hello"]; 

    [self method2];-----> // I access data from the array like, x=[self.substrings objectatindex:0]; 

    //finally, remove the items in the array 
    [self.substrings removeObject:@"hello"]; 
} 
Смежные вопросы