简体   繁体   English

Objective C NSMutableArray使用nil

[英]Objective C NSMutableArray work with nil

I want to create a NSMutableArray to represent a game board that holds grid element (assume I have a chess class). 我想创建一个NSMutableArray来表示一个包含网格元素的游戏板(假设我有一个国际象棋类)。 In this case, I already know the size of the board so I want to create an array by initWithCapacity:[size] and then initialize them as nil . 在这种情况下,我已经知道了电路板的大小,因此我想通过initWithCapacity:[size]创建一个数组,然后将其初始化为nil During the game, I may insert or remove chess object into/from this array based on game. 在游戏过程中,我可能会根据游戏将国际象棋对象插入或移除该数组。 I need to check if some cell is nil sometimes. 我需要检查某些单元格有时是否nil

Clearly initWithCapacity only allocate memory, but gives an empty array whose element is not assessable. 显然, initWithCapacity仅分配内存,但给出一个其元素不可评估的空数组。 I think insert [NSNull null] one by one is inefficient (I can turn to use 0 to represent nil but still not what I want). 我认为一个接一个地插入[NSNull null]效率不高(我可以转向使用0来表示nil但仍然不是我想要的)。

Is there any type/structure in Objective C like a C/C++ array for my purpose? 目的C中是否有任何类型/结构,例如C / C ++数组? Or is it wise to use C/C++ array here? 还是在这里使用C / C ++数组明智? (eg Chess myArray[size] ) (例如Chess myArray[size]

It is by design that NSMutableArray s and other collections do not permit nil and [NSNull null] is used as a placeholder for the concept of "nothing" instead. 根据设计NSMutableArray和其他集合不允许nil[NSNull null]用作“无”概念的占位符。

I think insert [NSNull null] one by one is inefficient 我认为[NSNull null]插入[NSNull null]效率低下

[NSNull null] is a singleton, so won't allocate lots of memory unnecessarily and is not inefficient. [NSNull null]是一个单例,因此不会不必要地分配大量内存,并且效率不高。

This answer has more information. 此答案有更多信息。


Anther efficiency I'm concerning about [NSNull null] is when I first create the array, if this array is kind of large (say 100 entries) 我关心的[NSNull null]花药效率是我第一次创建数组时,如果该数组很大(例如100个条目)

100 elements isn't a problem. 100个元素不是问题。 Your device will be able to iterate 100 elements very quickly. 您的设备将能够非常快速地迭代100个元素。 Time it if you like. 如果愿意,可以定时。

is enumeration the only way I can assign a nil to each of these 100 entries? 枚举是我可以为这100个条目中的每一个分配零的唯一方法吗? (eg for (i=0;i<size;i++) {[myArray addObject:[NSNull null]];} (例如, for (i=0;i<size;i++) {[myArray addObject:[NSNull null]];}

It's the first way I would think of doing it. 这是我想到的第一种方式。


"Premature optimisation is the root of all evil" “过早的优化是万恶之源”
Donald Knuth 唐纳德·克努斯

You seem to be concentrating on optimisation too early. 您似乎太早专注于优化。 You should favour readability over efficiency at this point. 此时,您应该优先考虑可读性而不是效率

When your app seems to slow down, or near the end of a release, profile the application and find out what, if any problems exist. 当您的应用程序似乎变慢或即将发布时,请对该应用程序进行概要分析,并找出可能存在的问题。 Otherwise you may find yourself spending a day "optimising" a for loop to find that you've saved 0.000001s per update. 否则,您可能会发现自己花了一天时间“优化” for循环,以发现每次更新节省了0.000001s。

Moreover, readable code is easier to: 此外,可读代码更易于执行以下操作:

  • debug 调试
  • update 更新
  • maintain 保持
  • share 分享

Micro-optimised code takes longer to produce, is prone to bugs , difficult to debug and maintain and often impossible to share as another developer may not know how to interpret your optimisations. 微优化的代码需要较长时间才能产生,很容易出现错误 ,难以调试维护 ,往往无法分享与其他开发人员可能不知道如何解释你的优化。

That's not to say "don't optimise", rather concentrate on optimising the biggest problems. 这并不是说“不乐观”,而是专注于优化最大的问题。

You can use something like this: 您可以使用如下形式:

NSMutableArray *array = [NSMutableArray arrayWithArray:@[@"First", @"Second", @"Third"]];
[array addObject:[NSNull null]];

[array enumerateObjectsUsingBlock:^(id obj, NSUInteger idx, BOOL *stop) {
    if (![obj isKindOfClass:[NSNull class]]) {
        // Do something
    }
}];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM