Become a MacRumors Supporter for $50/year with no ads, ability to filter front page stories, and private forums.

aarplane

macrumors newbie
Original poster
Dec 8, 2005
12
0
Canada
Hey guys,

Just wondering how I can make an array of strings using Obj-C or plain C. Any quick and easy methods?

Thanks in advance!
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
string is a pretty vague description. char * style strings in C? NSStrings in Objective-C? What sort of array? A plan C array? An NSMutableArray?

I will try to demonstrate both without mixing the types, because that's rarely what you want to do.

In C:

Code:
#include <stdio.h>

int main(int argc, char *argv[]) { //Argv is an array of char * strings, already.
  char myStringArray[100][1024];
  int x = 0;
  for(x=0; x<100; x++) {
    snprintf(myStringArray[x],1024,"This is string number %d!",x+1);
  }

  for(x=0; x<100; x++) {
    printf("String number %d has the value: %s\n",x+1,myStringArray[x]);
  }
  return 0;
}

In Objective-C:
Code:
#import <Foundation/Foundation.h>

int main(int argc, char *argv[]) {
  NSMutableArray *myMutableArray = [[NSMutableArray alloc] initWithCapacity:100];
  int x = 0;
  for(x=0; x<100;x++) {
    [myMutableArray insertObject: [NSString stringWithFormat:@"This is NSString number %d!",x+1] atIndex:x];
  }
  for(x=0; x<100;x++) {
    NSLog(@"NSString number %d is: %@",x+1,[myMutableArray objectAtIndex:x]);
  } 
  return 0;
}

I don't have access to an Objective-C runtime at the moment, but I think the second one is correct.

-Lee
 

Cromulent

macrumors 604
Oct 2, 2006
6,816
1,101
The Land of Hope and Glory
Well the most common way in C is:

char someVar[10][100];

which will give you 10 arrays of 100 characters each. Another method is:

char *somevar[10];

which will give you an array of 10 character pointers which can point to a memory location of different sizes, which is an advantage if your strings are going to be of widely differing size. You just access each string in the normal way though, someVar[2][7] is the same in both examples and syntactically legal.

Edit : Balls, beaten to it :).
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
In Objective-C:
Code:
#import <Foundation/Foundation.h>

int main(int argc, char *argv[]) {
  NSAutoreleasePool * pool = [[NSAutoreleasePool alloc] init];
  NSMutableArray *myMutableArray = [[NSMutableArray alloc] initWithCapacity:100];
  int x = 0;
  for(x=0; x<100;x++) {
    [myMutableArray insertObject: [NSString stringWithFormat:@"This is NSString number %d!",x+1] atIndex:x];
  }
  for(x=0; x<100;x++) {
    NSLog(@"NSString number %d is: %@",x+1,[myMutableArray objectAtIndex:x]);
  } 
  [pool release];
  return 0;
}

I amended the above to include an autorelease pool, forgot the first go round. I also checked it out now that I'm on my Mac and it seems to work properly.

There are quite a few options for doing this in C/Objective-C, but I tried to keep it simple. I set aside more than enough memory in the C version for safety's sake (as well as using snprintf). You can also do more advanced things like allocate memory for each string using char *s as Cromulent suggested or use char * literals assigned to char *s similar to lazydog's example of initialization at declaration.

-Lee
 

aarplane

macrumors newbie
Original poster
Dec 8, 2005
12
0
Canada
Let me post basically what I want to do.

I have this program written in Python (and using PyObjC), and I'm trying to "convert" it over to pure Objective-C.

Here is the code in Python (should be relatively easy to see what I want to do):

Code:
randomInteger = random.randint(1,3)

someArray = [ "entry1", "entry2", "entry3" ]
self.labelOut.setStringValue_(someArray[randomInteger-1])

labelOut is the name of the class outlet connected to a label in IB.

Any enlightenment?

Edit: Woot! Got it working.

For the curious (and to make sure I "get" it):
Code:
randomInteger = arc4random() % 3 + 1;

NSArray *someArray = [[NSArray alloc] initWithObjects: @"entry1", @"entry2", @"entry3", nil];
[labelOut setStringValue: [someArray objectAtIndex:randomInteger-1]];
 

lee1210

macrumors 68040
Jan 10, 2005
3,182
3
Dallas, TX
Let me post basically what I want to do.

I have this program written in Python (and using PyObjC), and I'm trying to "convert" it over to pure Objective-C.

Here is the code in Python (should be relatively easy to see what I want to do):

Code:
randomInteger = random.randint(1,3)

someArray = [ "entry1", "entry2", "entry3" ]
self.labelOut.setStringValue_(someArray[randomInteger-1])

labelOut is the name of the class outlet connected to a label in IB.

Any enlightenment?

Code:
#include <stdlib.h>
...
(void) setValue {
  int idx;
  srandom(time(NULL));
  idx=random() % 3;
  NSString *value = nil;
  select case(idx) {
    case(0): value=@"entry1"; break;
    case(1): value=@"entry2"; break;
    case(2): value=@"entry3"; break;
  }
  [myLabel setStringValue:value];
}


This doesn't use an NSArray, because for this sort of example it didn't seem worth while, but:

Code:
NSArray *myStringArray = [[NSArray alloc] initWithObjects: @"entry1", @"entry2", @"entry3",nil];
srandom(time(NULL));
[myLabel setStringValue:[myStringArray objectAtIndex:(random()%3)]];

Shows use of an NSArray to store the strings.

-Lee
 
Register on MacRumors! This sidebar will go away, and you'll see fewer ads.