Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Objective-c array

I am slightly confused as I am using this piece of code;

NSArray *dirContents = [[NSFileManager defaultManager] contentsOfDirectoryAtPath:@"/Volumes/" error:nil];

int arraysize = sizeof dirContents;

to get an array of the contents of the "Volumes" direcoty, however when I output the array size it says that the array has 8 entries when there are only 4 files in that directory? This wouldn't be a problem but since I am using a for loop as soon as I get to;

NSString *volume1 = [dirContents objectAtIndex:4];

(4 being the value in the for loop) the application crashes and refuses to launch?

Thanks for any help

like image 697
Lennart Avatar asked Jan 17 '23 22:01

Lennart


1 Answers

You should use int arraysize = [dirContents count] to get the correct size.

sizeof is a c-style operator that will not work correctly on Objective-C objects.

like image 197
Damien Avatar answered Jan 29 '23 03:01

Damien