2014-12-02 43 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"]; 
}