View Full Version : Array of Strings in Obj-C/C
aarplane
Aug 20, 2008, 04:24 PM
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
Aug 20, 2008, 04:42 PM
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:
#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:
#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
Aug 20, 2008, 04:52 PM
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 :).
lazydog
Aug 20, 2008, 05:39 PM
Hi
If it's an array of static strings that you want, then in C for example:-
char* strs[] = { "hello", "world", "!" } ;
b e n
lee1210
Aug 20, 2008, 07:59 PM
In Objective-C:
#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
Aug 20, 2008, 09:50 PM
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):
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):
randomInteger = arc4random() % 3 + 1;
NSArray *someArray = [[NSArray alloc] initWithObjects: @"entry1", @"entry2", @"entry3", nil];
[labelOut setStringValue: [someArray objectAtIndex:randomInteger-1]];
lee1210
Aug 20, 2008, 10:08 PM
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):
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?
#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:
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
vBulletin® v3.8.6, Copyright ©2000-2012, Jelsoft Enterprises Ltd.