#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;
}
#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;
}
char* strs[] = { "hello", "world", "!" } ;
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; }
randomInteger = random.randint(1,3)
someArray = [ "entry1", "entry2", "entry3" ]
self.labelOut.setStringValue_(someArray[randomInteger-1])
randomInteger = arc4random() % 3 + 1;
NSArray *someArray = [[NSArray alloc] initWithObjects: @"entry1", @"entry2", @"entry3", nil];
[labelOut setStringValue: [someArray objectAtIndex:randomInteger-1]];
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?
#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];
}
NSArray *myStringArray = [[NSArray alloc] initWithObjects: @"entry1", @"entry2", @"entry3",nil];
srandom(time(NULL));
[myLabel setStringValue:[myStringArray objectAtIndex:(random()%3)]];