简体   繁体   English

Objective-C中的数组

[英]Arrays in Objective-C

If you want to use an array of characters/badguys is it better to have one array of badguy objects and each object have properties like Badguy1: color=blue, health=80. 如果要使用一个字符/ badguys数组,最好有一个数组的badguy对象,并且每个对象都具有Badguy1之类的属性:color = blue,health = 80。 Then you loop through your array of characters and pull that information.... OR is it better to have multiple small arrays like character array, color array, health array and the index of all 3 arrays align to be the same character? 然后,您遍历字符数组并获取该信息。...或者将多个小数组(如字符数组,颜色数组,运行状况数组以及所有3个数组的索引对齐为同一字符)更好?

I know how to pull information from each array if it is separate but I do not know how to get the properties of each character if it is all wrapped up in 1 array. 我知道如何从每个数组中分离信息,但是如果每个字符都包装在一个数组中,我不知道如何获取每个字符的属性。

I ask just because it seems like it would make more sense to use a single array and pull the parts that you need. 我只是问,因为使用单个数组并提取所需的部分似乎更有意义。

Actually, now that I think about it. 实际上,现在我考虑了。 I guess I should just have an array of pointers and then get my values from the objects and not try to store all of that in the array. 我想我应该只拥有一个指针数组,然后从对象获取我的值,而不是尝试将所有值存储在数组中。

I know how to pull information from each array if it is separate but I do not know how to get the properties of each character if it is all wrapped up in 1 array. 我知道如何从每个数组中分离信息,但是如果每个字符都包装在一个数组中,我不知道如何获取每个字符的属性。 I ask just because it seems like it would make more sense to use a single array and pull the parts that you need. 我只是问,因为使用单个数组并提取所需的部分似乎更有意义。

You are correct in that it's generally smarter and a better design decision to keep related things together. 您是正确的,因为将相关内容放在一起通常是更明智和更好的设计决策。 If you'd like to store them this way, you can simply do: 如果您想以这种方式存储它们,则只需执行以下操作:

// Get the character at index 3 in the "characters" array
//   and print how many hit points it has left.
GameCharacter *ch = [characters objectAtIndex:3];
NSLog(@"This character has: %@ hit points", ch.hitPointsRemaining);

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

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