How to initialize NSArrays / NSMutableArrays when declaring properties

I need help understanding how to use NSArrays / NSMutableArrays as properties.

  • What property attributes should arrays be: strong or copied? In which case should I use which attribute?
  • How to initialize arrays in code.
  • If the array of properties should be NSArray or NSMutableArray

I am currently doing this in 2 different ways, as shown below. But all this is ambiguous and done without a clear understanding of mechanics. Which one is right or wrong.

Approach 1


.m file interface() @property (nonatomic, strong) NSMutableArray *arrayOfData; implementation <....other code> self.arrayOfData = [NSMutableArray arrayWithCapacity:count]; [self.arrayOfData addObject:<my object>] 

Approach 2


  .h file @property (nonatomic, strong) NSArray *arrayOfData; .m file //Property setter - (void)setListOfData:(NSMutableArray *)newList { if (_arrayOfData != newList) { _arrayOfData = [newList copy]; } } //Function which initializes the arrayOfData NSMutableArray *newData = [[NSMutableArray alloc] init]; .....<code which adds data> self.arrayOfData = newData; 
+4
source share
1 answer

NSArray or NSMutableArray

Well, whether to use NSArray or NSMutableArray highly dependent on your use case. Are you changing the contents of the array at runtime, but only a few elements here or there? Use NSMutableArray (ideal for UITableView editable data sources). Do you always reload the data source with completely new data? I would just use NSArray in this case.

strong or copy

It again depends on what you want to do. If, when someone calls a property that you want him to get his own copy of the data source, use copy . But I have never used it that way, and I think that for 90% of the cases of use, you would be better off using strong . This means that the caller .arrayOfData gets a pointer to an NSArray in your class (and can thus detect changes in it).

how to use

As I said, I usually use NSMutableArray for a UITableView data source. In the header file

 @property (nonatomic, strong) NSMutableArray *arrayOfData; 

the same as you. What I'm doing differently is that in the .m file I override getter to lazily create me an NSMutableArray . So I left the installer myself and executed the following getter implementation.

 - (NSMutableArray *) arrayOfData { if (!_arrayOfData) { _arrayOfData = [NSMutableArray new]; } return _arrayOfData; } 

So the first time you use self.arrayOfData in your class, it is allocated and entered and stored in the instance variable. Then, the second time the instance variable will simply return on it. Hope this cleared up a bit.

change

Usecase example: you have a Twitter client with a TwTableViewController showing a list of tweets. Let's say you have a private method called -(NSArray*)_getNewTweets , which will retrieve tweets from the server and return NSArray with them to you. For this, you also create the fetchNewData method. See Stream below.

TwTableViewController.h :

 @interface TwTableViewController : UITableViewController @property (nonatomic, strong) * downloadedTweets; - (void) fetchNewData; @end 

TwTableViewController.m :

 @implementation TwTableViewController - (NSMutableArray *) downloadedTweets { if (!_downloadedTweets) { _downloadedTweets = [NSMutableArray new]; } return _downloadedTweets; } - (NSArray *)_getNewTweets { NSArray * newTweets = ... //whatever magic to download new tweets return newTweets; } - (void) fetchNewData { [self.downloadedTweets addObjectsFromArray:[self _getNewTweets]]; [self.tableView reloadData]; //animated would be prettier, but out of this scope } - (void) viewDidLoad { [super viewDidLoad]; [self fetchNewData]; } //tableview stuff @end 

The first time self.downloadedTweets call self.downloadedTweets array will be created empty, and you can simply add / move / delete elements. There is no need to rewrite this array for another, one is enough. I used -addObjectsFromArray: but of course you can use -addObject: Clear now?

+5
source

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


All Articles