获取数组的大小
我有一些代码需要使用 for 循环从数组中读取变量。
int size=sizeof names;
NSLog(@"thelast one is %d",size);
NSString *usersName=userName.text;
NSString *usersPass=passWord.text;
for (i=0; i<=size;i++){
NSString *namesArray=[names objectAtIndex:i];
NSString *passArray=[pass objectAtIndex:i];
NSLog(@"namesArray %@",namesArray);
NSLog(@"passArray %@",passArray);
if([namesArray isEqualToString:usersName]){
userValid=1;
NSLog(@"The content of arry4 is %@",namesArray);
}
if([passArray isEqualToString:usersPass]){
passValid=1;
NSLog(@"The content of arry4 is %@",passArray);
}
else {
userValid=0;
passValid=0;
}
}
我遇到了一些问题,因为每次从程序内调用此函数时,几乎就好像“sizeof name”是错误的,因此并非检查数组中的所有值。 我通常是一名Java程序员,所以我习惯了names.length,并且我被告知sizeof名称本质上是同一件事......有什么帮助吗?
干杯。
i have some code that requires the use of a for loop to read variables from an array.
int size=sizeof names;
NSLog(@"thelast one is %d",size);
NSString *usersName=userName.text;
NSString *usersPass=passWord.text;
for (i=0; i<=size;i++){
NSString *namesArray=[names objectAtIndex:i];
NSString *passArray=[pass objectAtIndex:i];
NSLog(@"namesArray %@",namesArray);
NSLog(@"passArray %@",passArray);
if([namesArray isEqualToString:usersName]){
userValid=1;
NSLog(@"The content of arry4 is %@",namesArray);
}
if([passArray isEqualToString:usersPass]){
passValid=1;
NSLog(@"The content of arry4 is %@",passArray);
}
else {
userValid=0;
passValid=0;
}
}
I've been having some problems because every time this function is called from within the program, it's almost as if the 'sizeof names' is wrong, therefore not all values in the array are checked.
I'm generally a Java programmer so i'm used to names.length, and i was told sizeof names is essentially the same thing... any help?
Cheers.
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(5)
不要使用 sizeof。使用
[名称计数]
。Don't use sizeof. Use
[names count]
.您想使用 [names count] 而不是 sizeof 名称。 Sizeof 将为您提供实际名称对象指针本身的大小,而不是元素的数量,因为它是动态内存类型。
You want to use [names count] not sizeof names. Sizeof is going to give you the size of the actual names object pointer itself and not the number of elements, since it's dynamic memory type.
要获取存储在
NSAarray
中的元素数量,您应该使用实例方法count
,它返回一个NSUInteger
。或者,您可以使用 for in 循环来迭代这些元素,如果我没记错的话,这在 Java 中也可用。
请注意,
sizeof
是一个 C 运算符,它返回其操作数的大小(以字节为单位),因此它不会告诉您该NSArray
中存储了多少个元素,而是告诉您大小以一个 NSArray 实例的字节为单位。To get the number of elements stored in an
NSAarray
you should use the instance methodcount
, which returns anNSUInteger
.Alternatevely, you can iterate over these elements using the
for in
loop, which is available also in Java, if I recall correctly.Note that
sizeof
is a C operator that returns the size in bytes of its operand, so it doesn't tell you how many elements are stored in thatNSArray
, but the size in bytes of one NSArray instance.我知道你的问题已经得到解答 - 但这里有一种更可可的编写方式
I know your question has already been answered - but here is a more Cocoa way of writing it
还可以使用快速枚举,在某些情况下它可以让读者更清楚:
One can also use Fast Enumeration, in some cases it can be more clear to a reader: