Array of Strings in Obj-C/C

Discussion in 'Mac Programming' started by aarplane, Aug 20, 2008.

  1. aarplane macrumors newbie

    Joined:
    Dec 8, 2005
    Location:
    Canada
    #1
    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!
     
  2. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #2
    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
     
  3. Cromulent macrumors 603

    Cromulent

    Joined:
    Oct 2, 2006
    Location:
    The Land of Hope and Glory
    #3
    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 :).
     
  4. lazydog macrumors 6502a

    Joined:
    Sep 3, 2005
    Location:
    Cramlington, UK
    #4
    Hi

    If it's an array of static strings that you want, then in C for example:-

    Code:
    char* strs[] = { "hello", "world", "!" } ;
    
    b e n
     
  5. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #5
    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
     
  6. aarplane thread starter macrumors newbie

    Joined:
    Dec 8, 2005
    Location:
    Canada
    #6
    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]];
    
     
  7. lee1210 macrumors 68040

    lee1210

    Joined:
    Jan 10, 2005
    Location:
    Dallas, TX
    #7
    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
     

Share This Page