14

I am just getting into Obj C, and I am looking to create an array of MKAnnotations.

I have already created the MKAnnotation class called TruckLocation that contains the name, description, latitude, and longitude.

Here is what I have so far for the array:

NSMutableArray* trucksArray =[NSMutableArray arrayWithObjects: @[<#objects, ...#>]  nil];
novicePrgrmr
  • 18,647
  • 31
  • 81
  • 103

6 Answers6

22

Yore trying to combine 2 different syntaxes for similar but different things. You also don't seem to have any instances of your annotations.

Create some instances

TruckLocation *a1 = ...;
TruckLocation *a2 = ...;

Then we can add them

NSMutableArray *trucksArray = [NSMutableArray arrayWithObjects:a1, a2, nil];

Or

NSMutableArray *trucksArray = [@[a1, a2] mutableCopy]

This is a shorter and more modern form but you need to make it mutable as it will create an immutable instance.

boulder_ruby
  • 38,457
  • 9
  • 79
  • 100
Wain
  • 118,658
  • 15
  • 128
  • 151
  • I don't understand the mutableCopy method. I've seen it around and it's cousin - the immutable copy of a mutable object. what's the purpose of copying and why is it so often seen with mutable arrays and dictionaries? – noobsmcgoobs Jun 12 '14 at 09:31
  • 1
    @noobsmcgoobs if you have an immutable object and you want to modify it you need to create a new object, that could either be with a convenience method or by making a mutable copy and editing it (usually if you want to make multiple modifications). You might immutable copy to keep a record of something you were given and protecting yourself against someone changing that object behind your back... – Wain Jun 12 '14 at 13:46
17

Well:

NSString *a = @"a";
NSMutableArray *array = [NSMutableArray arrayWithObjects:a,nil];
//or
NSMutableArray *array = [[NSMutableArray alloc]init]; //alloc

[array addObject:a];
Nagarjun
  • 6,557
  • 5
  • 33
  • 51
oiledCode
  • 8,589
  • 6
  • 43
  • 59
8
NSMutableArray *array = [NSMutableArray alloc] init];
[array addObject:myObject];

Where myObject is the object of your custom class.

Juan Catalan
  • 2,299
  • 1
  • 17
  • 23
1

Try something lke this

  NSMutableArray *annotationArray=[[NSMutableArray alloc]init];

        CLLocationCoordinate2D shopPosition = CLLocationCoordinate2DMake(allShopInfoObject.shopLatitudeValue, allShopInfoObject.shopLongitudeValue);

    MapAnnotation *mapAnnotation = [[MapAnnotation alloc] initWithCoordinates:shopPosition andTitle:allShopInfoObject.shopName andShopId:allShopInfoObject.shopId subTitle:@""];

        [annotationArray addObject:mapAnnotation];
[self.mapView addAnnotations:annotationArray];
Vinodh
  • 5,262
  • 4
  • 38
  • 68
0

Suppose you initialize the objects and assign values the following way:

TruckLocation *truckLocationOne = [[TruckLocation alloc]initWithAnnotation:annotation
           reuseIdentifier:annotationIdentifier];
truckLocationOne.name = @"name";

TruckLocation *truckLocationTwo = [[TruckLocation alloc]initWithAnnotation:annotation
           reuseIdentifier:annotationIdentifier];
truckLocationTwo.name = @"name";

These objects would be added to the array temp in the following way:

1) NSMutableArray temp = [[NSMtableArray alloc]initWithObjects:truckLocationOne,truckLocationTwo,nil];

2) NSMutableArray temp = [[NSMtableArray alloc]init]; [temp addObject:truckLocationOne]; [temp addObject:truckLocationTwo];

Hope this answers your query

Macrosoft-Dev
  • 2,195
  • 1
  • 12
  • 15
0
TruckLocation *loc1=...;
TruckLocation *loc2=...;
NSMutableArray *truckArray=[[NSMutableArray alloc]initWithObjects:loc1,loc2];

you can add objects at the time of initialization. by this you can add objects within the allocation code. also you can avoid more number of steps for writing with addObject.

Soorej Babu
  • 350
  • 2
  • 19