Iphone 目标C:在向地图添加注释时执行错误访问
我有以下目标C代码:Iphone 目标C:在向地图添加注释时执行错误访问,iphone,objective-c,Iphone,Objective C,我有以下目标C代码: NSString *urlStr=[[NSString alloc] initWithFormat:@"http://www.prestocab.com/driver/ajax/getFriendsOnMap.php"]; NSURL *url=[NSURL URLWithString:urlStr]; __block ASIFormDataRequest *request=[[ASIFormDataRequest alloc ]initWithURL:u
NSString *urlStr=[[NSString alloc] initWithFormat:@"http://www.prestocab.com/driver/ajax/getFriendsOnMap.php"];
NSURL *url=[NSURL URLWithString:urlStr];
__block ASIFormDataRequest *request=[[ASIFormDataRequest alloc ]initWithURL:url];
[request setDelegate:self];
[request setPostValue:[NSString stringWithFormat:@"%f",swCoord.latitude ] forKey:@"sw_lat"];
[request setPostValue:[NSString stringWithFormat:@"%f",swCoord.longitude ] forKey:@"sw_lng"];
[request setPostValue:[NSString stringWithFormat:@"%f",neCoord.latitude ] forKey:@"ne_lat"];
[request setPostValue:[NSString stringWithFormat:@"%f",neCoord.longitude ] forKey:@"ne_lng"];
[request setCompletionBlock:^{
NSLog(@"%@",[request responseString]);
SBJsonParser *parser=[[SBJsonParser alloc]init];
//NSDictionary *obj=[parser objectWithString:[request responseString] error:nil];
NSDictionary *arr=[parser objectWithString:[request responseString] error:nil];
MapViewAnnotation *annotation=[[MapViewAnnotation alloc]init];
for(int i=0;i<arr.count;i++){
NSDictionary *obj=[arr objectForKey:[NSString stringWithFormat:@"%d",i]];
CLLocationCoordinate2D coord;
coord.latitude=[[obj objectForKey:@"lat"] doubleValue];
coord.longitude=[[obj objectForKey:@"lng"] doubleValue];
[annotation initWithTitle:[obj objectForKey:@"uname"] andCoordinate:coord];
//[self.mapView performSelectorOnMainThread:@selector(addAnnotation) withObject:annotation waitUntilDone:YES];
[self.mapView addAnnotation:annotation];
}
[annotation release];
//[self.mapView addAnnotations:annotations];
//[annotations release];
}];
[request setFailedBlock:^{
}];
[request startAsynchronous];
NSString*urlStr=[[NSString alloc]initWithFormat:@”http://www.prestocab.com/driver/ajax/getFriendsOnMap.php"];
NSURL*url=[NSURL URLWithString:urlStr];
__block ASIFormDataRequest*请求=[[ASIFormDataRequest alloc]initWithURL:url];
[请求setDelegate:self];
[请求setPostValue:[NSString stringWithFormat:@“%f”,swCoord.latitude]forKey:@“sw_lat”];
[请求setPostValue:[NSString stringWithFormat:@“%f”,swCoord.longide]forKey:@“sw_lng”];
[请求setPostValue:[NSString stringWithFormat:@“%f”,neCoord.latitude]forKey:@“ne_lat”];
[request setPostValue:[NSString stringWithFormat:@“%f”,neCoord.longitude]forKey:@“ne_lng”];
[请求setCompletionBlock:^{
NSLog(@“%@,[请求-响应]);
SBJsonParser*parser=[[SBJsonParser alloc]init];
//NSDictionary*obj=[parser objectWithString:[request responseString]错误:nil];
NSDictionary*arr=[parser objectWithString:[request responseString]错误:nil];
MapViewAnnotation*annotation=[[MapViewAnnotation alloc]init];
对于(int i=0;i您获得了错误的访问权限,因为地图视图或注释已被破坏,但您有一个指向该实例的指针。我猜您没有保留地图视图。您可以随时检查此类错误
或者将此代码放在发生错误的代码行之前:
NSLog(@"mapView-desc: %@",[self.mapView description]);
NSLog(@"annotation-desc: %@",[annotation description]);
坏的_访问现在应该发生在这两行中的一行,然后您就知道忘记保留哪个obejct;)如果一切正常,则问题出在mapView内部或批注中的数据。最简单的方法是启用NSZombies
并在控制台中等待消息。完成block
运行时self
可能无效。在这种情况下,您需要将block
复制到堆中代码>
您可以包装块:
[ <your block> copy];
[复制];
然后是释放块的问题,很多时候自动释放效果很好:
[[ <your block> copy] autorelease];
[[copy]自动释放];
其他时候,您可能需要显式地释放它
您可能需要typedef
您的块来更清楚地说明这一点。您正在反复使用同一个注释实例。不知道这里的API,我只能猜测,但它闻起来像是一个错误。好的,我将注释实例放在for循环中,仍然会得到相同的错误…可能无法解决您的问题,但您没有正在泄漏大量内存。循环中的init方法([annotation initWithTitle:[obj objectForKey:@“uname”]and Coordinate:coord];
)每次都添加+1计数,但您没有用一个释放来平衡每个init。您好,您能告诉我怎么做吗?嘿,非常感谢。我发现,如果我使用initWithTitle:@“asdf”工作正常!但是,如果我将@“asdf”替换为[obj objectForKey:@“uname”],它会崩溃!我已经对[obj objectForKey:@“uname”]进行了NSLog,看起来还可以!confusedConstant字符串具有保留计数,因此它们永远不会释放。[obj objectForKey:@“uname”]
返回一个自动释放的对象。这表示在您使用字符串之前正在调用runloop,并且自动释放导致对象释放。您需要保留字符串,直到块完成。我不使用ASI*代码,对此无能为力。好的,这有点道理,但是您有什么建议吗我该如何解决这个问题?你可能知道我只是一个新手iPhone程序员…嗨,当我这么做的时候,我得到了以下mapView描述:2011-09-25 18:35:19.010 14[48323:e903]注释描述:啊,好的,然后我们必须进一步搜索一个发布的对象。首先:在编译期间是否有任何警告?并启用zombies并在异常时停止,并显示代码片段和由zombie抛出的消息。如果在没有NSzombie消息的情况下抛出坏的\u访问,这也是一条有趣的信息。我将来看看这个启用僵尸的东西。这对我来说是新的。谢谢你的帮助!启用僵尸非常容易。如果启用了僵尸,每个应该被销毁的对象都会被一个僵尸对象所取代。如果你正在访问zdeallocated对象,你会得到一个僵尸,它会立即引发异常。这会告诉你是哪个对象您正在访问在正常模式下已经解除分配的内容,以及代码中的位置。