开发者

Getting the size of an array

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]){开发者_Go百科
        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.


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


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.


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.


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;
}


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;
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜