Iphone 如何释放即将返回的变量?

Iphone 如何释放即将返回的变量?,iphone,objective-c,Iphone,Objective C,当我在Xcode中点击build菜单上的“build and Analysis”按钮时,我遇到了一个问题。分析建议我释放一个我希望稍后返回的变量。代码如下所示: - (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{ //I do some other thing here MKPinAnnotationView

当我在Xcode中点击build菜单上的“build and Analysis”按钮时,我遇到了一个问题。分析建议我释放一个我希望稍后返回的变量。代码如下所示:

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{

     //I do some other thing here

     MKPinAnnotationView *annView=
        [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"addressLocation"];

     //I do some other thing here

     return annView;
}
return [myVariable autorelease];
-(MKAnnotationView*)地图视图:(MKMapView*)地图视图注释:(id)注释{
//我在这里做其他事情
MKPinAnnotationView*annView=
[[MKPinAnnotationView alloc]initWithAnnotation:annotation重用标识符:@“addressLocation”];
//我在这里做其他事情
返回视图;
}

我可以在不引起任何问题的情况下释放annView并返回它吗?

您是否查看了
自动释放

您是否查看了
自动释放

基于您在此处发布的内容,不,您不能释放它然后返回它。警告是,如果要在其他代码中对对象设置任何其他保留。在使用nslog语句返回之前,您可以轻松地对此进行检查


NSLog(@“重新计入:%d”,[annView重新计入]

根据您在此处发布的内容,不,您不能先发布它,然后再返回。警告是,如果要在其他代码中对对象设置任何其他保留。在使用nslog语句返回之前,您可以轻松地对此进行检查


NSLog(@“重新计入:%d”,[annView重新计入]

这正是自动释放的目的。这种方法应该自动释放它


如果你对这类事情不清楚的话,我建议你读一下报纸。它很短,很好地解释了所有这些东西。一旦你理解了这个指南,你就再也不用怀疑了。

这正是自动释放的目的。这种方法应该自动释放它


如果你对这类事情不清楚的话,我建议你读一下报纸。它很短,很好地解释了所有这些东西。一旦您理解了该指南,您就不必再疑惑了。

这里有解释自动释放池的有用Lynda.com视频:

http://creativemac.digitalmedianet.com/articles/viewarticle.jsp?id=1003156

可从以下网址获得解释自动释放池的有用Lynda.com视频:

http://creativemac.digitalmedianet.com/articles/viewarticle.jsp?id=1003156

自动释放池是您使用的最佳选择。返回变量时,请执行以下操作:

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{

     //I do some other thing here

     MKPinAnnotationView *annView=
        [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"addressLocation"];

     //I do some other thing here

     return annView;
}
return [myVariable autorelease];

苹果的很多方法都使用这种方法。apple类上的大多数静态构造函数,如[NSString stringWithFormat:]返回自动释放变量。

自动释放池是您使用的最佳选择。返回变量时,请执行以下操作:

- (MKAnnotationView *) mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>) annotation{

     //I do some other thing here

     MKPinAnnotationView *annView=
        [[MKPinAnnotationView alloc] initWithAnnotation:annotation reuseIdentifier:@"addressLocation"];

     //I do some other thing here

     return annView;
}
return [myVariable autorelease];

苹果的很多方法都使用这种方法。apple类上的大多数静态构造函数,如[NSString stringWithFormat:]返回自动释放的变量。

我想到了它。我只是不确定它在这种情况下是否有效。如果我错了,请告诉我,因为我是objective-c的新手。@Winston Chen:这正是
autorelease
的目的。我想到了这一点。我只是不确定它在这种情况下是否有效。如果我错了,请告诉我,因为我是objective-c的新手。@Winston Chen:这正是
autorelease
的目的。检查保留计数不是一个好主意。它不能给你任何东西的准确图像。检查保留计数不是一个好主意。它不能给你一个准确的图片,谢谢。那很有帮助,谢谢。这很有帮助。