0

I want to use MutableArray for any method i was announce on the top with following code

    @implementation STARTTRIP_1

    NSMutableArray *path = [NSMutableArray array];
...

but it's was error "Initializer element is not a compile-time constant"

I want to use this array for contain all string.

    - (void)motionDetector:(SOMotionDetector *)motionDetector locationChanged:(CLLocation *)location
...            

if (numberFormatter == nil) {
                numberFormatter = [[NSNumberFormatter alloc] init];
                numberFormatter.numberStyle = NSNumberFormatterDecimalStyle;
                numberFormatter.maximumFractionDigits = 6;
            }
            NSString *string = [NSString stringWithFormat:@"{%@, %@}",
                                [numberFormatter stringFromNumber:[NSNumber numberWithDouble:coordinate.latitude]],
                                [numberFormatter stringFromNumber:[NSNumber numberWithDouble:coordinate.longitude]]];
            [path addObject:string]; //HERE
            NSLog(@"%@",path);
...
  • You can't execute code outside of a method. The statement must be in a method. `[NSMutableArray array]` is a method call on the class. – zaph Apr 13 '14 at 13:23

1 Answers1

2

I presume you are trying to create a static variable?

Create your path in load as this is called when the class is first loaded.

static NSMutableArray *path;

+(void)load
{
    [super load];
    path = [NSMutableArray array];
}

-(void)method
{

    // use path
}

Though to be honest I imagine you're doing something a bit nasty - why not use a property or an iVar?

@implementation STARTTRIP_1 {
    NSMutableArray *_path;
}

-(instancetype)init
{
    self = [super init];
    if (self) { 
        _path = [NSMutableArray array];
    }
    return self;
}
Community
  • 1
  • 1
Rich
  • 8,108
  • 5
  • 46
  • 59