Reputation: 195
i'm NEW TO XCODE, working with mapkit and annotations, howver after building and running fine, the application crashes on load. I ran the debugger and;
Stopped at Breakpoint 1 'mapView:viewForAnnotation: - Line 951'
continues after a few hit counts,
Program received signal: "EXC_BAD_ACCESS".
I've read countless solutions something to do with alloc or releasing or something but have no idea where the problem is in my code.
EDIT: PROBLEM SOLVED.
Upvotes: 0
Views: 462
Reputation: 17906
mapView:viewForAnnotation
could be dramatically shorter, without using plists or collections. A reuse identifier is useful for dequeueing annotations of a particular type or annotations that perhaps have expensive-but-identical setup work. You're initializing every one of your annotations the same way, so they can all use the same reuse identifier without issue. The function could thus be written:
- (MainViewAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id <MKAnnotation>)annotation
{
if(annotation == mapView.userLocation) return nil;
NSString* identifier = @"City";
MainViewAnnotationView *newAnnotationView = (MainViewAnnotationView *)[self.mapView dequeueReusableAnnotationViewWithIdentifier:identifier];
if(nil == newAnnotationView)
{
newAnnotationView = [[[MainViewAnnotationView alloc] initWithAnnotation:myAnnotation reuseIdentifier:identifier] autorelease];
}
[newAnnotationView setEnabled:YES];
[newAnnotationView setCanShowCallout:YES];
return newAnnotationView;
}
Upvotes: 1
Reputation: 736
Seriously too much code to go through.
Here is how you can figure these errors.
a) Enable "Stop on Objective-C exceptions" in Run menu of XCode. This will get you pretty close to where the error occurred. Just look at the stack and you will be able to figure.
b) If that does not help, then try the NSZombie route although I have never had a need to use it. http://www.cocoadev.com/index.pl?DebuggingAutorelease . I mostly figure by just reviewing the code.
Hope this helps.
Upvotes: 1
Reputation: 2413
Not a direct answer to your question, but i highly recommend you use a switch statement to replace the long "else if" code. Makes your code look much cleaner and your compiler can do optimizations.
http://en.wikipedia.org/wiki/Switch_statement
Edit; After looking at the other comments (from Eiko), I have to agree that in this case you even want to remove this whole piece of code and replace it with some collection. The code contains too much duplications.
Upvotes: 2