I want to created and populate an Array in a class and then use the content in the array in the main program… As an example I’ll use a list of names of the US States…
This is an update on the original question that is halfway resolved and has been contented for clarity so you can see the code properly.
ArrayStates.h
#import <Foundation/Foundation.h>
@interface ArrayStates : NSObject
@end
ArrayStates.m
#import "ArrayStates.h"
@interface ArrayStates() { NSMutableArray *listOfStates; }
@implementation ArrayStates
- (void)viewDidLoad {
//---initialize the array---
listOfStates = [[NSMutableArray alloc] init];
//---add items---
[listOfStates addObject:@"ALABAMA"];
[listOfStates addObject:@"WYOMING"];
[super viewDidLoad];
}
@end
This is updated code from the previous Question but it still generates a Missing end error on the implementation line, the void wants a . after method prototype and you still can’t reference the object in the main program.
I believe altering the ordering if the interface and implementation differentiates whether the array can be accessed within or outside the class (thanks to iHungry for that).
1 Answer