0

我有这个代码:

-(void)handleLongPressGesture:(UIGestureRecognizer*)sender {

NSNumber* existingpoints = [[NSNumber alloc]init];


 existingpoints =[NSNumber numberWithInt:0]; 


// This is important if you only want to receive one tap and hold event
if (sender.state == UIGestureRecognizerStateEnded)
{
    [self.mapView removeGestureRecognizer:sender];
}
else {

    do {
        int z = 1;
        existingpoints =[NSNumber numberWithInt:z];

        // Here we get the CGPoint for the touch and convert it to latitude and longitude coordinates to display on the map
        CGPoint point = [sender locationInView:self.mapView];
        CLLocationCoordinate2D locCoord = [self.mapView convertPoint:point toCoordinateFromView:self.mapView];
        // Then all you have to do is create the annotation and add it to the map

        MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init]; annotationPoint.coordinate = locCoord;



        NSString *latitude = [[NSString alloc] initWithFormat:@"%f",locCoord.latitude];


        NSString *longitude = [[NSString alloc] initWithFormat:@"%f", locCoord.longitude];


        annotationPoint.title = @"Event";
        annotationPoint.subtitle = [NSString stringWithFormat:@"%@ & %@", latitude, longitude];

        [mapView addAnnotation:annotationPoint];


        [[NSUserDefaults standardUserDefaults]setObject:latitude forKey:@"FolderLatitude"];
        [[NSUserDefaults standardUserDefaults]setObject:longitude forKey:@"FolderLongitude"];


    } while ([existingpoints intValue] == 0);

        }
}

...但问题是,当我按住时,然后拖动不止一个针脚。我只想添加一个引脚。所以我尝试了do方法,但它不起作用。我无法理解,因为当我执行代码时,我将 NSNumber 的值变为 1,而 while 说 = 0 来运行代码。

请帮忙!!

4

1 回答 1

0

您当前的代码很容易出现大量内存泄漏。例如:

NSNumber* existingpoints = [[NSNumber alloc] init];
existingpoints = [NSNumber numberWithInt:0];

泄漏是因为您将第一个实例existingpoints保留为 1 而没有在任何地方释放它。除非你使用 ARC。您只需一条指令即可优化上述代码:

NSNumber* existingpoints = [NSNumber numberWithInt:0];

如果您需要将它保存在某个地方,请保留它(但我相信事实并非如此)。

分析代码,我建议不要将现有点用作NSNumber. 使用 anNSInteger代替(它不是一个对象,只是一个 typedef to long)。

这是我重写的代码:

-(void)handleLongPressGesture:(UIGestureRecognizer*)sender {
    NSInteger existingpoints = 0;

    // This is important if you only want to receive one tap and hold event
    if (sender.state == UIGestureRecognizerStateEnded) {
        [self.mapView removeGestureRecognizer:sender];
    }
    else {
        do {
            int z = 1;
            existingpoints = z;

            // Here we get the CGPoint for the touch and convert it to latitude and longitude coordinates to display on the map
            CGPoint point = [sender locationInView:self.mapView];
            CLLocationCoordinate2D locCoord = [self.mapView convertPoint:point toCoordinateFromView:self.mapView];

            // Then all you have to do is create the annotation and add it to the map
            MKPointAnnotation *annotationPoint = [[MKPointAnnotation alloc] init];
            annotationPoint.coordinate = locCoord;

            NSString *latitude = [NSString stringWithFormat:@"%f",locCoord.latitude];
            NSString *longitude = [NSString stringWithFormat:@"%f", locCoord.longitude];

            annotationPoint.title = @"Event";
            annotationPoint.subtitle = [NSString stringWithFormat:@"%@ & %@", latitude, longitude];

            [mapView addAnnotation:annotationPoint];

            [[NSUserDefaults standardUserDefaults] setObject:latitude forKey:@"FolderLatitude"];
            [[NSUserDefaults standardUserDefaults] setObject:longitude forKey:@"FolderLongitude"];

            [annotationPoint release]; // Remove this if you're using ARC.
        } while (existingpoints == 0);
    }
}

请注意,我还更改了使用 ARC 时创建latitudelongitude不创建任何内存泄漏的代码。

编辑:进一步分析您的代码,我不明白为什么这种方法会一次丢弃两个引脚。也许您可以检查您的方法是否没有被调用两次?

更多:如果你只想运行一次,为什么还要有一个 do/while 循环?(但也许你只是在为更进一步的发展铺平道路)

于 2012-06-22T17:28:11.033 回答