Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <h1>NSArray or NSMutableArray</h1> <p>Well, whether to use <code>NSArray</code> or <code>NSMutableArray</code> depends heavily on your use case. Are you changing the contents of the array during runtime but only few items here or there? Use <code>NSMutableArray</code> (perfect for editable <code>UITableView</code>s data sources). Do you always reload the data source with completely new data? I'd just use <code>NSArray</code> in that case.</p> <h1>strong or copy</h1> <p>That again depends on what you want to do. If, when someone calls the property you want them to get their own copy of the data source, use <code>copy</code>. But I have actually never used it this way and I think for 90 % of the use cases you will be better off using <code>strong</code>. That means that the caller of <code>.arrayOfData</code> gets a pointer to the <code>NSArray</code> in your class (and can thus detect changes to it).</p> <h1>how to use</h1> <p>As I mentioned, I usually use <code>NSMutableArray</code> for like a <code>UITableView</code> data source. In the header file I have </p> <pre><code>@property (nonatomic, strong) NSMutableArray *arrayOfData; </code></pre> <p>same as you. What I do differently is that in the <code>.m</code> file, I override the <code>getter</code> to lazily create me a <code>NSMutableArray</code>. I would thus leave the setter alone and put the following <code>getter</code> implementation.</p> <pre><code>- (NSMutableArray *) arrayOfData { if (!_arrayOfData) { _arrayOfData = [NSMutableArray new]; } return _arrayOfData; } </code></pre> <p>This way, the first time you use <code>self.arrayOfData</code> in your class, it gets allocated and inited and saved into the instance variable. Then from the second time on it will just keep returning the instance variable. Hope this cleared it up a bit.</p> <h2>edit</h2> <p>Sample usecase: You have a Twitter client with <code>TwTableViewController</code> showing the list of tweets. Let's say you will have a private method called <code>-(NSArray*)_getNewTweets</code> that will fetch the tweets from the server and return you an <code>NSArray</code> with them. To do this thing you create a <code>fetchNewData</code> method also. See the flow below.</p> <p><code>TwTableViewController.h</code>:</p> <pre><code>@interface TwTableViewController : UITableViewController @property (nonatomic, strong) * downloadedTweets; - (void) fetchNewData; @end </code></pre> <p><code>TwTableViewController.m</code>:</p> <pre><code>@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 </code></pre> <p>The first time you call <code>self.downloadedTweets</code>, the array will get created empty and you can just keep adding/moving/removing items. No need to overwrite this array for another one, one is enough. I used <code>-addObjectsFromArray:</code> but of course you can use <code>-addObject:</code> also. Is it clearer now?</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload