Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

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];
share|improve this question
add comment

5 Answers

up vote 3 down vote accepted

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.

share|improve this answer
add comment

Well

NSString *a = @"a";
NSMutableArray *array = [NSMutableArray arrayWithObjects:a,nil];
// or
NSMutableArray *array = [NSMutableArray alloc]init];
[array addObject:a];
share|improve this answer
add comment
NSMutableArray *array = [NSMutableArray alloc] init];
[array addObject:myObject];

Where myObject is the object of your custom class.

share|improve this answer
add comment

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

share|improve this answer
add comment

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];
share|improve this answer
add comment

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.