You need to show more code, but the problem is pretty obvious if that really is the line that is erroring out.
You can only dynamically initialization variables at the time of declaration in very specific spots. Dynamically includes calling a method.
NSMutableArray *a = [NSMutableArray array]; // this will error.
static NSMutableArray *a = [NSMutableArray array]; // this will error.
@implementation Booger
{
NSMutableArray *a = [NSMutableArray array]; // this will error
}
NSMutableArray *a = [NSMutableArray array]; // this will error.
- (void)bar
{
NSMutableArray *a = [NSMutableArray array]; // this is fine
}
Sounds like you need to dive a bit more deeply on the whole object-oriented thing. A class is a collection of functions called methods that either operate on the class (class methods) or a single instance of the class (instance methods). An instance can store state that is accessible to all methods when any method is invoked on that instance. In traditional OO, such state is stored in instance variables. Typically, you would define a pair instance methods that set and get that instance variable's value. These are called accessors or setter/getter. In modern Objective-C, we use properties to declare both the instance variables and the methods that access the instance variable.
Thus:
@interface MyClass:NSObject
@property(strong) NSMutableArray *myArray;
@end
@implementation MyClass
// the @property will automatically create an instance variable called _myArray,
// a getter method called -myArray and a setter called -setMyArray:
- init
{
self = [super init];
if (self) {
_myArray = [NSMutableArray array]; // set the ivar directly in init
}
return self;
}
- (void)maybeAddThisThing:(Thing *)aThing
{
if ([aThing isCool] && ![[self myArray] containsObject:aThing]) {
[[self myArray] addObject:aThing];
}
}
- (void)nukeFromOrbit
{
[self setMyArray:[NSMutableArray array]];
// or you could do [[self myArray] removeAllObjects];
}
images
. Most likely it is a global or static variable so you can't assign the value like you are. – rmaddy Jul 19 '13 at 0:52static NSMutableArray* images= [NSMutableArray array]
? – Ramy Al Zuhouri Jul 19 '13 at 0:53@implementation
line? After? If after, is it inside curly braces or not? – rmaddy Jul 19 '13 at 1:13