获取数组的大小

发布于 2024-11-15 18:49:36 字数 866 浏览 4 评论 0原文

我有一些代码需要使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(5

深海夜未眠 2024-11-22 18:49:36

不要使用 sizeof。使用[名称计数]

Don't use sizeof. Use [names count].

⒈起吃苦の倖褔 2024-11-22 18:49:36

您想使用 [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.

云巢 2024-11-22 18:49:36

要获取存储在 NSAarray 中的元素数量,您应该使用实例方法 count,它返回一个 NSUInteger

或者,您可以使用 for in 循环来迭代这些元素,如果我没记错的话,这在 Java 中也可用。

for (MyClass *element in myArray) {
    NSLog(@"%@", element);
}

请注意,sizeof 是一个 C 运算符,它返回其操作数的大小(以字节为单位),因此它不会告诉您该 NSArray 中存储了多少个元素,而是告诉您大小以一个 NSArray 实例的字节为单位。

To get the number of elements stored in an NSAarray you should use the instance method count, which returns an NSUInteger.

Alternatevely, you can iterate over these elements using the for in loop, which is available also in Java, if I recall correctly.

for (MyClass *element in myArray) {
    NSLog(@"%@", element);
}

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 that NSArray, but the size in bytes of one NSArray instance.

微凉徒眸意 2024-11-22 18:49:36

我知道你的问题已经得到解答 - 但这里有一种更可可的编写方式

NSString *userName = userName.text;
NSString *userPass = passWord.text;

// Use a block enumerator
NSUInteger nameIdx = [names indexOfObjectPassingTest:^(id obj, NSUInteger idx, BOOL *stop) {
    return ([obj isEqualToString:userName]);
}];

// Is the name in the array
if (nameIdx == NSNotFound) {
    // Name not in array - so set to zero
    userValid = 0;
    passValid = 0;
} else {
    userValid = 1;
    // See if the corresponding password is correct
    NSString password = [pass objectAtIndex:nameIdx];
    if (![password isEqualToString:userPass]) {
        passValid = 0;
    } else {
        passValid = 1;
}

I know your question has already been answered - but here is a more Cocoa way of writing it

NSString *userName = userName.text;
NSString *userPass = passWord.text;

// Use a block enumerator
NSUInteger nameIdx = [names indexOfObjectPassingTest:^(id obj, NSUInteger idx, BOOL *stop) {
    return ([obj isEqualToString:userName]);
}];

// Is the name in the array
if (nameIdx == NSNotFound) {
    // Name not in array - so set to zero
    userValid = 0;
    passValid = 0;
} else {
    userValid = 1;
    // See if the corresponding password is correct
    NSString password = [pass objectAtIndex:nameIdx];
    if (![password isEqualToString:userPass]) {
        passValid = 0;
    } else {
        passValid = 1;
}
何处潇湘 2024-11-22 18:49:36

还可以使用快速枚举,在某些情况下它可以让读者更清楚:

NSString *userName = userName.text;
NSString *userPass = passWord.text;

BOOL userValid = NO;
BOOL passValid = NO;
int index = 0;
for (NSString *eachName in namesArray) {
    if ([eachName isEqualToString:userName) {
        userValid = YES:
        if ([[passArray objextAtIndex:index] isEqualToString:passWord) {
            passValid = YES;
        }
        break;
    }
    index += 1;
}

One can also use Fast Enumeration, in some cases it can be more clear to a reader:

NSString *userName = userName.text;
NSString *userPass = passWord.text;

BOOL userValid = NO;
BOOL passValid = NO;
int index = 0;
for (NSString *eachName in namesArray) {
    if ([eachName isEqualToString:userName) {
        userValid = YES:
        if ([[passArray objextAtIndex:index] isEqualToString:passWord) {
            passValid = YES;
        }
        break;
    }
    index += 1;
}
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文