Is there any quick way I can get the number of strings within a NSString array?
NSString *s[2]={@"1", @"2"}
I want to retrieve the length of 2 from this. I there something like (s.size) I know there is the -length method but that is for a string not a string array. I am new to Xcode please be gentle.
Use NSArray
NSArray *stringArray = [NSArray arrayWithObjects:@"1", @"2", nil];
NSLog(@"count = %d", [stringArray count]);
Yes, there is a way. Note that this works only if the array is not created dynamically using malloc
.
NSString *array[2] = {@"1", @"2"}
//size of the memory needed for the array divided by the size of one element.
NSUInteger numElements = (NSUInteger) (sizeof(array) / sizeof(NSString*));
This type of array is typical for C, and since Obj-C is C's superset, it's legal to use it. You only have to be extra cautious.
sizeof(s)/sizeof([NSString string]);
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With