#import <Foundation/Foundation.h>
int main(int argc, char *argv[]) {
NSAutoreleasePool *pool = [[NSAutoreleasePool alloc] init];
NSArray *myListOfStrings = [NSArray arrayWithObjects:@"My First String",@"My Second String",@"My Third String",@"My Fourth String",@"My Fifth String",nil];
for(int x=0;x<[myListOfStrings count];x++) {
NSLog(@"String %d is %@",x+1,[myListOfStrings objectAtIndex:x]);
}
[pool release];
return 0;
}
error: initializer element is not constant
#import "AppController.h"
NSArray *myListOfStrings = [NSArray arrayWithObjects:@"Chullll ******",@"Big Kitchen? FAGOT!",@"Best sause ever. Cost $20",@"You should become a veggitarian",@"$400 BB GUNS!", @"Your TV sucks. Not HD!", @"Kar 98K with Double tap!", nil];
@implementation AppController
- (IBAction)buttonClicked:(id)sender
{
[response setStringValue:[NSString stringWithFormat:@"I have been clicked"]];
}
@end
[NSString stringWithFormat:@"I have been clicked"]
@"I have been clicked"
[NSString stringWithFormat:@"I have been clicked"]
-(void)awakeFromNib
-(void)awakeFromNib {
myListOfStrings = [NSArray arrayWithObjects:@"String 1",@"String 2",@"String 3",@"String 4",@"String 5", @"String 6", @"String 7", nil];
}
NSArray *myListOfStrings;
Why doesnt that work? Im trying to change the label to one of my values in the array
Well, before you go too far here, it seems as though you now have an NSArray declared in global scope which is very unusual for a Cocoa program and probably not what you wanted.
And I don't think you retained the array, either.
If you need to keep the array around for a while, you probably want to create an instance variable in your AppController and then initialize that in awakeFromNib or just in your init as Lee as shown. Don't forget to retain it also.
[response setStringValue:[myListOfStrings objectAtIndex:(random()%8)]];
#import "AppController.h"
@implementation AppController
- (void)awakeFromNib {
NSArray *myListOfStrings = [NSArray arrayWithObjects:@"s1",@"s2",@"s3",@"s4",
@"s5", @"s6", @"s7", @"s8", nil];
}
- (IBAction)buttonClicked:(id)sender
{
[response setStringValue:[myListOfStrings objectAtIndex:(random()%8)]];
}
@end
#import <Cocoa/Cocoa.h>
@interface AppController : NSObject {
IBOutlet NSTextField *response;
NSArray *myListOfStrings;
}
- (IBAction)buttonClicked:(id)sender;
@end
[myListOfStrings retain];
randomize();
-(NSString *)getRandomQuote
{
NSUInteger myPos = random()%[myListOfStrings count];
return [myListOfStrings objectAtIndex:myPos];
}
[response setStringValue:[self getRandomQuote]];
@implementation AppController
- (void)awakeFromNib {
NSArray *myListOfStrings = [NSArray arrayWithObjects:@"s1",@"s2",@"s3",@"s4",
@"s5", @"s6", @"s7", @"s8", nil];
[myListOfStrings retain];
randomize();
}
-(NSString *)getRandomQuote
{
return [myListOfStrings objectAtIndex:(random()%[myListOfStrings count])];
}
- (IBAction)buttonClicked:(id)sender
{
[response setStringValue:[self getRandomQuote]];
}
@end
warning: local declaration of 'myListOfStrings' hides instance variable
warning: implicit declaration of function 'randomize'
#include <stdlib.h>
myListOfStrings = [NSArray arrayWithObjects:@"s1",@"s2",@"s3",@"s4",
@"s5", @"s6", @"s7", @"s8", nil];