How to load plist and pass string Array polygons under NSDictionary?
I am trying to load data.plist string array polygons under NSDictionary(ex. +40.48675,-60.434543) to building. I have 6 index in array. I keep getting warning "Passing argument 7 of initWithID:name:code:number:location:description:polygons from incompatible pointer type." Also the draw polygon doesnt show on map. What ami I missing?
NSString *path = [[NSBundle mainBundle] pathForResource:@"data" ofType:@"plist"];
NSArray *array = [[[NSArray alloc] initWithContentsOfFile:path] objectAtIndex:0];
buildings = [[NSMutableArray alloc] init];
for(int i = 0; i < [array count]; i++)
{
NSMutableDictionary *dic = [array objectAtIndex:i];
NSString *buildingID = [dic objectForKey:@"buildingID"];
NSString *name = [dic objectForKey:@"name"];
NSString *code = [dic objectForKey:@"code"];
NSString *number = [dic objectForKey:@"number"];
//NSString *location = [dic objectForKey:@"location"];
NSString *description = [dic objectForKey:@"description"];
//NSArray *components = [location componentsSeparatedByString:@","];
//NSString *latitude = [components objectAtIndex:0];
//NSString *longitude = [components objectAtIndex:1];
NSArray *polygons = [[NSArray alloc] init];
polygons = [dic objectForKey:@"polygons"];
int countPoints = [self.polygons count];
CLLocationCoordinate2D buildingPoly[countPoints]开发者_运维百科;
for (int i = 0; i < countPoints; i++) {
NSArray *splitArrayPoly = [[polygons objectAtIndex:i] componentsSeparatedByString:@","];
buildingPoly[i] = CLLocationCoordinate2DMake([[splitArrayPoly objectAtIndex:0] floatValue], [[splitArrayPoly objectAtIndex:1] floatValue]);
}
NSArray *splitArray = [ [dic objectForKey:@"location"] componentsSeparatedByString:@","];
float latitude = [[splitArray objectAtIndex:0]floatValue];
float longitude = [[splitArray objectAtIndex:1]floatValue];
CLLocation *tempLocation = [[[CLLocation alloc]
initWithLatitude:latitude
longitude:longitude]autorelease];
Building *building = [[[Building alloc] initWithID:[buildingID intValue]
name:name
code:code
number:number
location:tempLocation
description:description
polygons:buildingPoly]autorelease]; //This is where I got warning
//building.polygons = polygons; // delete this when ure done with init
//NSLog(@"building.polygons = %@", building.polygons); // ask teacher about this
[buildings addObject:building];
[dic release];
[building release];
}
Then in different class "Building.m" it pass polygons to here
- (id)initWithID:(int)b_id
name:(NSString *)b_name
code:(NSString *)b_code
number:(NSString *)b_number
location:(CLLocation *)b_location
description:(NSString *)b_description
polygons:(NSArray *)b_polygons
{
self = [super init];
if (self != nil) {
self.buildingID = b_id;
self.name = b_name;
self.location = b_location;
self.code = b_code;
self.number = b_number;
self.description = b_description;
self.polygons = b_polygons;
}
return self;
}
Then this method should get all polygons and make it on map. However it doesn't show anything.
- (MKPolygon *)getPolygons{
int countPoints = [self.polygons count];
CLLocationCoordinate2D building[countPoints];
for (int i = 0; i < countPoints; i++) {
NSArray *splitArray = [[polygons objectAtIndex:i] componentsSeparatedByString:@","];
building[i] = CLLocationCoordinate2DMake([[splitArray objectAtIndex:0] floatValue], [[splitArray objectAtIndex:1] floatValue]);
}
return [MKPolygon polygonWithCoordinates:building count:countPoints];
}
According to your own definition the 7 argument to initWithID should be an NSArray*
- (id)initWithID:(int)b_id
name:(NSString *)b_name
code:(NSString *)b_code
number:(NSString *)b_number
location:(CLLocation *)b_location
description:(NSString *)b_description
polygons:(NSArray *)b_polygons;
... but when you call it you pass buildingPoly ...
Building *building = [[[Building alloc] initWithID:[buildingID intValue]
name:name
code:code
number:number
location:tempLocation
description:description
polygons:buildingPoly]autorelease];
... and buildingPoly is ...
CLLocationCoordinate2D buildingPoly[countPoints];
which is not an NSArray.
You should really pay attention to warnings. They often are telling you fairly directly where problems are.
精彩评论