gpt4 book ai didi

iphone - “MyAnnotation”可能无法响应 'initWithDictionary:'

转载 作者:行者123 更新时间:2023-11-29 04:33:32 25 4
gpt4 key购买 nike

我不断遇到此代码的语义问题(“MyAnnotation”可能不会响应“initWithDictionary:”),我使用 plist 将注释添加到 map 。

尽管它显示了 pin 和我想要的一切,但我遇到了语义问题并且似乎无法解决问题

如果有人能提供帮助,那就太好了

这是代码,问题出在//BrewMapViewController.m 中,错误就在这一行

MyAnnotation *annotation = [[MyAnnotation alloc] initWithDictionary:breweryDict];

/*MyAnnotation.h*/
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface MyAnnotation : NSObject <MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
NSString *test;
}

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@property (nonatomic, copy) NSString *test;


@end


/*MyAnnotation.m*/
#import "MyAnnotation.h"
@implementation MyAnnotation
@synthesize coordinate, title, subtitle, test;

- (id) initWithDictionary:(NSDictionary *) dict
{
self = [super init];
if (self != nil) {
coordinate.latitude = [[dict objectForKey:@"latitude"] doubleValue];
coordinate.longitude = [[dict objectForKey:@"longitude"] doubleValue];
self.title = [dict objectForKey:@"name"];
self.subtitle = [dict objectForKey:@"address"];
self.test = [dict objectForKey:@"test"];
}
return self;
}


@end


/*BrewMapViewController.h*/
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>

@interface BrewMapViewController : UIViewController <MKMapViewDelegate> {
IBOutlet MKMapView *map;

NSArray *breweries;
}

@end


/*BrewMapViewController.m*/

#import "BrewMapViewController.h"

#import "MyAnnotation.h"

@implementation BrewMapViewController


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad {
[super viewDidLoad];

breweries = [[NSArray alloc] initWithContentsOfFile:[[NSBundle mainBundle]
pathForResource:@"test"
ofType:@"xml"]];

double minLat = [[breweries valueForKeyPath:@"@min.latitude"] doubleValue];
double maxLat = [[breweries valueForKeyPath:@"@max.latitude"] doubleValue];
double minLon = [[breweries valueForKeyPath:@"@min.longitude"] doubleValue];
double maxLon = [[breweries valueForKeyPath:@"@max.longitude"] doubleValue];

MKCoordinateRegion region;
region.center.latitude = (maxLat + minLat) / 2.0;
region.center.longitude = (maxLon + minLon) / 2.0;
region.span.latitudeDelta = (maxLat - minLat) * 1.05;
region.span.longitudeDelta = (maxLon - minLon) * 1.05;
map.region = region;

for (NSDictionary *breweryDict in breweries){
MyAnnotation *annotation = [[MyAnnotation alloc] initWithDictionary:breweryDict];
[map addAnnotation:annotation];
[annotation release];
}
}


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

if (map.userLocation == annotation){
return nil;
}

NSString *identifier = @"MY_IDENTIFIER";

MKAnnotationView *annotationView = [map dequeueReusableAnnotationViewWithIdentifier:identifier];
if (annotationView == nil){
annotationView = [[[MKAnnotationView alloc] initWithAnnotation:annotation
reuseIdentifier:identifier]
autorelease];
annotationView.image = [UIImage imageNamed:@"beer.png"];
annotationView.canShowCallout = YES;

annotationView.rightCalloutAccessoryView = [UIButton buttonWithType:UIButtonTypeDetailDisclosure];

annotationView.leftCalloutAccessoryView = [[[UIImageView alloc] initWithImage:[UIImage imageNamed:@"pretzel.png"]] autorelease];

}
return annotationView;
}

- (void)mapView:(MKMapView *)mapView annotationView:(MKAnnotationView *)view calloutAccessoryControlTapped:(UIControl *)control
{
NSLog(@"I've been tapped");
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
return YES;
}


- (void)dealloc {
[breweries release];
[map release];
[super dealloc];
}

@end

最佳答案

您必须将 - (id) initWithDictionary:(NSDictionary *) dict 的方法签名放入头文件中,以便告诉 BrewMapViewController 该方法存在:

/*MyAnnotation.h*/
#import <Foundation/Foundation.h>
#import <MapKit/MapKit.h>

@interface MyAnnotation : NSObject <MKAnnotation> {
CLLocationCoordinate2D coordinate;
NSString *title;
NSString *subtitle;
NSString *test;
}

@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
@property (nonatomic, copy) NSString *title;
@property (nonatomic, copy) NSString *subtitle;
@property (nonatomic, copy) NSString *test;

- (id) initWithDictionary:(NSDictionary *) dict;

@end

关于iphone - “MyAnnotation”可能无法响应 'initWithDictionary:',我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11370623/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com