2

How can I get the numbers of elements in a macro definition like this:

#define myThings @"A",@"B",@"C",@"D",@"E"

What is the way to get the answer 5 from myThings?

[myThings count] does not work...

Brian Tompsett - 汤莱恩
  • 5,753
  • 72
  • 57
  • 129
Gik
  • 528
  • 5
  • 18
  • your `[myThings count]` and `@"A"` and nsstring make me think here we are dealing with Obj-C... maybe that tag could be more appropriate than nsstring - or numbers? – ShinTakezou May 13 '12 at 17:45

1 Answers1

1

Defines are literal substitution of text: would it work a code like

  [@"A", @"B", @"C" count];

? If the answer is no, it won't work, then here it is your problem. You could do

  [[NSArray arrayWithObjects: myThings, nil] count];

This should work, but I do not suggest to use this "pattern" at all.

ADD

Another way to count how many values there are, could be the following:

  NSString *arr[] = {myThings};
  size_t cnt = sizeof(arr)/sizeof(NSString *);

this is a C common way to know how many elements are in arr. Again, I stress the fact that myThings is literally replaced by whatever you defined it using #define, and so the result is the same as

  NSString *arr[] = {@"A", @"B", @"C", @"D", @"E"};

Add2

Note that, if you won't use arr "for real", optimizations will remove it, and there's no "waste of space".

However, another solution would be to count the number of arguments passed to a macro, so that CountingMacro(myThings) would be replaced with 5. This SO answer might help in this case.

Community
  • 1
  • 1
ShinTakezou
  • 9,432
  • 1
  • 29
  • 39
  • The problem is that "myThings" is a NSString, so count does not work. I don't want to create an array, just to count them (waste of memory). What would you suggest? – Gik May 13 '12 at 18:43
  • as you've written it, myThings is not a NSString: it is (literaly) a common separated list of NSString, which is different! I'm going to update the answers with another possibility – ShinTakezou May 13 '12 at 19:09
  • Got it. Thanx. But let me then ask for _your suggestion_ in my "problem": What I want is do define the items (in a .h file) and use them in this: **segmentedButton = [[UISegmentedControl alloc] initWithItems:[NSArray arrayWithObjects:myThings,nil]];** But, then (in another ViewController.m file) I want to know the number of myThings. I understand what you suggest (I did it and it works _of course_), but I am asking if that is the most efficient way to do it, cos it looks like a waste of memory to create an array space, just to count the items. Is there a better way? – Gik May 13 '12 at 21:21
  • if it is a statically created array, I bet it is not that big; moreover, if the array is static, you know in advance how many strings there are (5 in the example); but you're right it's better the cmpiler counts them; but it can't, unless you "create" something it can operate on; but **don't worry**: if your temporary array is not used elsewhere, optimizations will remove it from the final code, leaving only the computed "length". Another solution could be given here http://stackoverflow.com/questions/2308243/macro-returning-the-number-of-arguments-it-is-given-in-c – ShinTakezou May 13 '12 at 21:38