I know Apple is big on having you use NS
objects instead of true primitive types, but I need the capabilities of an array (namely direct access of items at indices). However, it seems that they are so very keen on using NS
objects that I can't find a single tutorial online or in a textbook about how to use basic primitive arrays. I want something that does things like this does in Java:
我知道苹果很喜欢让你使用NS对象而不是真正的原始类型,但我需要数组的功能(即直接访问索引上的项)。然而,他们似乎非常热衷于使用NS对象,以至于我在网上找不到任何关于如何使用基本基元数组的教程或教科书。我想要在Java中做这样的事情:
String inventory[] = new String[45];
inventory[5] = "Pickaxe";
inventory[12] = "Dirt";
inventory[8] = "Cobblestone";
inventory[12] = null;
System.out.println("There are " + inventory.length + " slots in inventory: " + java.util.Arrays.toString(inventory));
The following is the closest I've gotten in Objective-C, but it won't run properly:
下面是我在Objective-C中得到的最接近的结果,但是不能正常运行:
NSString *inventory[45];
inventory[5] = @"Pickaxe";
inventory[12] = @"Dirt";
inventory[8] = @"Cobblestone";
inventory[12] = nil;
NSArray *temp = [NSArray arrayWithObjects:inventory count:45];
NSLog(@"There are %i slots in inventory: %@", [temp count], [temp description]);
Also, if at all possible, is there something in O-C that will give me the count of non-null/non-nil objects in the array? (This way, I can tell how much space is left in the inventory so that the player can't pack away anything if it's full)
而且,如果可能的话,在O-C中是否有什么东西会给我数组中非空/非空对象的计数?(通过这种方式,我可以知道库存里还剩下多少空间,这样玩家在满了的时候就不能打包了)
2 个解决方案
#1
2
typically, you would use NSArray
/NSMutableArray
, although you could also use C arrays.
通常,您将使用NSArray/NSMutableArray,尽管您也可以使用C数组。
NSArray
(and most Foundation collections) cannot contain nil
entries - you can use NSPointerArray
(OS X) if you need nil
values, or simply use [NSNull null]
in an NSArray
to designate nil
.
NSArray(和大多数基础集合)不能包含nil条目——如果需要nil值,可以使用NSPointerArray (OS X),或者在NSArray中使用[NSNull null]来指定nil。
Here's your program using an NSArray
:
这是你的程序使用NSArray:
NSMutableArray * inventory = [NSMutableArray array];
for (NSUInteger idx = 0; idx < 45; ++idx) {
[inventory addObject:[NSNull null]];
}
[inventory replaceObjectAtIndex:5 withObject:@"Pickaxe"];
[inventory replaceObjectAtIndex:12 withObject:@"Dirt"];
[inventory replaceObjectAtIndex:8 withObject:@"Cobblestone"];
[inventory replaceObjectAtIndex:12 withObject:[NSNull null]];
NSArray *temp = [NSArray arrayWithObject:inventory];
NSLog(@"There are %i slots in inventory: %@", [temp count], [temp description]);
Also, if at all possible, is there something in O-C that will give me the count of non-null/non-nil objects in the array?
同样,如果可能的话,在O-C中是否有什么东西能给我数组中非空/非空对象的计数?
An NSArray is a CFArray - they are "toll free bridged". Use CFArrayGetCountOfValue
with [NSNull null]
as the value to seek. Most of the CF-APIs are available in the NS-APIs, but not this one. You could easily wrap it in a category, if needed often.
NSArray是CFArray——他们是“无桥桥”。使用带有[NSNull null]的CFArrayGetCountOfValue作为要查找的值。大多数cf - api都可以在nsapi中使用,但是这一个没有。如果需要的话,你可以很容易地把它归类。
#2
2
Use c-style arrays. Any c code will work in objective c. Try not to mix objective c style objects with c style code unless you are very careful. The memory management stuff gets weird, and it is easy to get confused on object types vs. primitives.
使用c风格的数组。任何c代码都可以在objective c中工作。除非您非常小心,否则不要将objective c风格的对象与c风格的代码混合使用。内存管理的东西变得很奇怪,很容易混淆对象类型和原语。
#1
2
typically, you would use NSArray
/NSMutableArray
, although you could also use C arrays.
通常,您将使用NSArray/NSMutableArray,尽管您也可以使用C数组。
NSArray
(and most Foundation collections) cannot contain nil
entries - you can use NSPointerArray
(OS X) if you need nil
values, or simply use [NSNull null]
in an NSArray
to designate nil
.
NSArray(和大多数基础集合)不能包含nil条目——如果需要nil值,可以使用NSPointerArray (OS X),或者在NSArray中使用[NSNull null]来指定nil。
Here's your program using an NSArray
:
这是你的程序使用NSArray:
NSMutableArray * inventory = [NSMutableArray array];
for (NSUInteger idx = 0; idx < 45; ++idx) {
[inventory addObject:[NSNull null]];
}
[inventory replaceObjectAtIndex:5 withObject:@"Pickaxe"];
[inventory replaceObjectAtIndex:12 withObject:@"Dirt"];
[inventory replaceObjectAtIndex:8 withObject:@"Cobblestone"];
[inventory replaceObjectAtIndex:12 withObject:[NSNull null]];
NSArray *temp = [NSArray arrayWithObject:inventory];
NSLog(@"There are %i slots in inventory: %@", [temp count], [temp description]);
Also, if at all possible, is there something in O-C that will give me the count of non-null/non-nil objects in the array?
同样,如果可能的话,在O-C中是否有什么东西能给我数组中非空/非空对象的计数?
An NSArray is a CFArray - they are "toll free bridged". Use CFArrayGetCountOfValue
with [NSNull null]
as the value to seek. Most of the CF-APIs are available in the NS-APIs, but not this one. You could easily wrap it in a category, if needed often.
NSArray是CFArray——他们是“无桥桥”。使用带有[NSNull null]的CFArrayGetCountOfValue作为要查找的值。大多数cf - api都可以在nsapi中使用,但是这一个没有。如果需要的话,你可以很容易地把它归类。
#2
2
Use c-style arrays. Any c code will work in objective c. Try not to mix objective c style objects with c style code unless you are very careful. The memory management stuff gets weird, and it is easy to get confused on object types vs. primitives.
使用c风格的数组。任何c代码都可以在objective c中工作。除非您非常小心,否则不要将objective c风格的对象与c风格的代码混合使用。内存管理的东西变得很奇怪,很容易混淆对象类型和原语。