声明为属性时如何初始化 NSArrays/NSMutableArrays [英] How to initialize NSArrays / NSMutableArrays when declared as properties

查看:74
本文介绍了声明为属性时如何初始化 NSArrays/NSMutableArrays的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我需要一些帮助来理解如何使用 NSArrays/NSMutableArrays 作为属性.

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

  1. 数组应该是什么属性属性:strong 还是 copy ?在什么情况下我应该使用哪个属性?
  2. 如何在代码中初始化数组.
  3. 我的属性数组应该是 NSArray 还是 NSMutableArray

目前我的做法有以下两种不同的方式.但这一切都是零散的,并且在对其机制没有明确理解的情况下完成.哪一个是正确的或不正确的.

Currently the way I am doing it 2 different ways as below. But it is all patchy and done with no clear understanding of the mechanics of it. Which one of it is correct or incorrect.

方法一

 .m file
    interface() 
           @property (nonatomic, strong) NSMutableArray *arrayOfData;
    implementation
         <....other code>

          self.arrayOfData = [NSMutableArray arrayWithCapacity:count];
         [self.arrayOfData addObject:<my object>]

方法二

 .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;

推荐答案

NSArray or NSMutableArray

好吧,是使用 NSArray 还是 NSMutableArray 在很大程度上取决于您的用例.您是否在运行时更改了数组的内容,但这里或那里只更改了几项?使用 NSMutableArray(非常适合可编辑的 UITableViews 数据源).您是否总是使用全新的数据重新加载数据源?在这种情况下,我只会使用 NSArray.

NSArray or NSMutableArray

Well, whether to use NSArray or NSMutableArray depends heavily on your use case. Are you changing the contents of the array during runtime but only few items here or there? Use NSMutableArray (perfect for editable UITableViews data sources). Do you always reload the data source with completely new data? I'd just use NSArray in that case.

这又取决于你想做什么.如果有人调用属性时,您希望他们获得自己的数据源副本,请使用 copy.但我实际上从未以这种方式使用过它,我认为对于 90% 的用例,使用 strong 会更好.这意味着 .arrayOfData 的调用者获得了一个指向你的类中的 NSArray 的指针(从而可以检测到它的变化).

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 copy. But I have actually never used it this way and I think for 90 % of the use cases you will be better off using strong. That means that the caller of .arrayOfData gets a pointer to the NSArray in your class (and can thus detect changes to it).

正如我提到的,我通常使用 NSMutableArray 作为 UITableView 数据源.在头文件中我有

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

@property (nonatomic, strong) NSMutableArray *arrayOfData;

和你一样.我所做的不同之处在于,在 .m 文件中,我覆盖了 getter 以懒惰地为我创建一个 NSMutableArray.因此,我将不理会 setter 并放置以下 getter 实现.

same as you. What I do differently is that in the .m file, I override the getter to lazily create me a NSMutableArray. I would thus leave the setter alone and put the following getter implementation.

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

这样,当您第一次在类中使用 self.arrayOfData 时,它会被分配和初始化并保存到实例变量中.然后从第二次开始它只会继续返回实例变量.希望这能稍微澄清一下.

This way, the first time you use self.arrayOfData 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.

示例用例:您有一个 Twitter 客户端,带有 TwTableViewController 显示推文列表.假设您将拥有一个名为 -(NSArray*)_getNewTweets 的私有方法,它将从服务器获取推文并返回一个带有它们的 NSArray.为此,您还需要创建一个 fetchNewData 方法.请参阅下面的流程.

Sample usecase: You have a Twitter client with TwTableViewController showing the list of tweets. Let's say you will have a private method called -(NSArray*)_getNewTweets that will fetch the tweets from the server and return you an NSArray with them. To do this thing you create a fetchNewData method also. See the flow 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

第一次调用 self.downloadedTweets 时,数组将被创建为空,您可以继续添加/移动/删除项目.无需为另一个数组覆盖此数组,一个就足够了.我使用了 -addObjectsFromArray: 但当然你也可以使用 -addObject: .现在清楚了吗?

The first time you call self.downloadedTweets, 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 -addObjectsFromArray: but of course you can use -addObject: also. Is it clearer now?

这篇关于声明为属性时如何初始化 NSArrays/NSMutableArrays的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆