= nil;
PolygonZone *polygonZone
= nil;
Location *myLocation = nil;
for (count=0;count!=length;++count)
{
NSInteger shape = [[[allObjects objectAtIndex:count] objectForKey:@"Shape"] integerValue];
if (shape == 0) //多边形
{
polygonZone =(PolygonZone*)[NSEntityDescription insertNewObjectForEntityForName:@"PolygonZone" inManagedObjectContext:self.myManageObjectContext];
NSDictionary* boundary = [[allObjects objectAtIndex:count] objectForKey:@"Boundary"];
NSArray* points = [boundary objectForKey:@"points"];
NSInteger pointsNum = points.count,i;
for (i=0;
i!=pointsNum; i++)
{
CLLocationDegrees latitude = [[[points objectAtIndex:i] objectForKey:@"latitude"] doubleValue];
CLLocationDegrees longitude = [[[points objectAtIndex:i] objectForKey:@"longitude"] doubleValue];
CLLocationCoordinate2D location = CLLocationCoordinate2DMake(latitude,
longitude);
MKMapPoint point = MKMapPointForCoordinate(location);
myLocation = (Location*)
[NSEntityDescription insertNewObjectForEntityForName:@"Location" inManagedObjectContext:self.myManageObjectContext];
myLocation.longitude = [NSNumber numberWithDouble:longitude];
myLocation.latitude = [NSNumber numberWithDouble:latitude];
[polygonZone addPointsObject:myLocation];
}
}
else //圆形
{
//取半径
NSDictionary* boundary = [[allObjects objectAtIndex:count] objectForKey:@"Boundary"];
CLLocationDegrees radius = [[boundary objectForKey:@"radius"] doubleValue];
//取圆心坐标
NSDictionary* center = [boundary objectForKey:@"center"];
CLLocationDegrees latitude = [[center objectForKey:@"latitude"] doubleValue];
CLLocationDegrees longitude = [[center objectForKey:@"longitude"] doubleValue];
CLLocationCoordinate2D location = CLLocationCoordinate2DMake(latitude,
longitude);
circleZone =(CircleZone*)[NSEntityDescription insertNewObjectForEntityForName:@"CircleZone" inManagedObjectContext:self.myManageObjectContext];
circleZone.longitude = [NSNumber numberWithDouble:longitude];
circleZone.latitude = [NSNumber numberWithDouble:latitude];
circleZone.radius = [NSNumber numberWithDouble:radius];
}
NSInteger Id = [[[allObjects objectAtIndex:count] objectForKey:@"Id"] integerValue];
NSString *name = [[allObjects objectAtIndex:count] objectForKey:@"Name"];
NSInteger belong = [[[allObjects objectAtIndex:count] objectForKey:@"Belong"] integerValue];
NSInteger type = [[[allObjects objectAtIndex:count] objectForKey:@"Type"] integerValue];
if (circleZone != nil)
{
circleZone.zoneid = [NSNumber numberWithInt:Id];
circleZone.name = [NSString stringWithString:name];
circleZone.belong = [NSNumber numberWithInt:belong];
circleZone.shape = [NSNumber numberWithInt:shape];
else{
polygonZone.zoneid = [NSNumber numberWithInt:Id];
polygonZone.name = [NSString stringWithString:name];
polygonZone.belong = [NSNumber numberWithInt:belong];
polygonZone.shape = [NSNumber numberWithInt:shape];
}
NSError *error;
if ([self.myManageObjectContext save:&error]
== NO )
{
NSLog(@"This was an error!");
}
else
{
NSLog(@"Save succeed!");
}
circleZone = nil;
polygonZone = nil;
}//for
结果按照zoneid升序顺序 ***********
= [NSEntityDescription entityForName:@"Zone" inManagedObjectContext:self.objectContext];
NSFetchRequest
*request = [[NSFetchRequest alloc] init];
NSSortDescriptor *sortDis
= [[NSSortDescriptor alloc] initWithKey:@"zoneid" ascending:YES];
NSPredicate *predicate = [NSPredicate predicateWithFormat:@"shape
= %@",[NSNumber numberWithInt:1]];
[request setEntity:discription];
NSError *error = nil;
NSArray *array = [self.objectContext executeFetchRequest:request error:&error];
> 0) {
Zone *zone = (Zone*)[array objectAtIndex:0];
zone.name = @"被修改了";
}
if (![self.objectContext save:&error])
{
NSLog(@"There was
an error!");
}
}
Zone *zone = nil;
if ([array count]]
> 0) {
zone = (Zone*)[array objectAtIndex:0];
}
if (zone)
{
[self.objectContext deleteObject:zone];
}
if ([self.objectContext hasChanges])
{
if (![self.objectContext save:&error])
{
NSLog(@"There
was an error!");
}