2014-12-02 36 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"]; 
} 
相關問題