Reputation: 841
I'm new to Obj-C and iPhone SDK. The test application I'm stock with is a color switcher containing two buttons ("Back", "Forward") and one text label. The idea is to switch between rainbow colors (background) and setting an appropriate text label in a cyclic manner.
I declared NSArray (which is to contain colors names) in RainbowViewController.h, synthesized it in RainbowViewController.h and I can't add any string into that array.
This is "h" file:
#import <UIKit/UIKit.h>
@interface RainbowViewController : UIViewController {
IBOutlet UILabel *currentColorTextLabel;
NSArray *colorsArray;
NSString *msg;
}
@property (nonatomic, retain) IBOutlet UILabel *currentColorTextLabel;
@property (nonatomic, retain) NSArray *colorsArray;
@property (nonatomic, retain) NSString *msg;
- (IBAction) pressForwardButton;
- (IBAction) pressBackButton;
@end
This is "m" file:
#import "RainbowViewController.h"
#import <Foundation/Foundation.h>
@implementation RainbowViewController
@synthesize currentColorTextLabel;
@synthesize colorsArray;
@synthesize msg;
int currentArrayIndex = 0;
colorsArray = [[NSArray alloc] init]; //here i get "Initializer element is not constant" error message
[coloursArray addObject:@"Red"]; //here I get "Expected identifier or '(' before '[' token"
[coloursArray addObject:@"Orange"];
//etc
- (IBAction) pressForwardButton {
//here I'm going to increment currentArrayIndex, set an appropriate color, and update a currentColorTextLabel based on currentArrayIndex.
}
- (IBAction) pressBackButton {
}
//auto-genereted code here
@end
Upvotes: 0
Views: 3448
Reputation: 21
jasongetsdown is correct. You need to instantiate the NSArray object with the objects it will contain and nil terminated.
@"Red", @"Blue", nil
If you wish to have an array that you can change you need to make it a Mutable Array.
However, you have another problem here. Your property that you are synthesizing and allocating for is an object named colorsArray and you are trying to pass a method to a coloursArray object, two different spellings.
Upvotes: 1
Reputation: 1305
I'm new to obj-c as well, but I think you need to initialize the array with objects, or use an NSMutableArray if you want to add objects after it is created.
Upvotes: 3
Reputation: 237110
You have the code that should go in your init method just sitting out in the middle of the file. You can't set instance variables like that.
Upvotes: 2