我仍在努力进行 ios 开发,但我希望你能帮助我。
目前我有一个 WCF,它以以下格式返回一些 json 数据
"Address": "3453453",
"Category": "CONCRETE",
"Closest_Property_Number": 2,
"ID": 42,
"Image1": 324,
"Image2": 0,
"Image3": 0,
"Latitude": 2,
"Longitude": "-6.541902",
"Notes": "GHTFHRG",
"User_ID": 2
然后我创建了一个名为 Location 的类,这里是 Location.m
#import "Location.h"
@implementation Location {
NSString* _address;
NSString* _category;
NSString* _closest_Property_Number;
NSString* _iD;
NSString* _image1;
NSString* _latitude;
NSString* _longitude;
NSString* _notes;
NSString* _user_ID;
}
@synthesize address = _address;
@synthesize category = _category;
@synthesize closest_Property_Number = _closest_Property_Number;
@synthesize iD = _iD;
@synthesize image1 = _image1;
@synthesize latitude = _latitude;
@synthesize longitude = _longitude;
@synthesize notes = _notes;
@synthesize user_ID = _user_ID;
@end
我认为到目前为止这是对的吗?这是我的课程,所有的导入都发生在这里
#import "Location.h"
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any additional setup after loading the view.
NSString *urlAsString = @"http://crm.fpmccann.co.uk/TemperatureWebService/iphonewebservice.svc/retrievelocations";
NSURL *url = [NSURL URLWithString:urlAsString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[NSURLConnection
sendAsynchronousRequest:urlRequest
queue:[[NSOperationQueue alloc] init]
completionHandler:^(NSURLResponse *response,
NSData *data,
NSError *error)
{
if ([data length] >0 && error == nil)
{
NSMutableArray* tmpLocations = [[NSMutableArray alloc] init];
for (NSDictionary* loc in locations) {
Location* location = [[Location alloc] initWithParameters:loc];
[tmpLocations addObject:location];
}
NSMutableArray* tmpAnnotations;
for (NSDictionary* location in tmpLocations)
{
// retrieve latitude and longitude from location
MKPointAnnotation* annotation = [[MKPointAnnotation alloc] init];
annotation.title = location.address;
newAnnotation.coordinate = location;
[tmpAnnotations addObject:annotation];
}
dispatch_async(dispatch_get_main_queue(), ^{
self.locations = tmpLocations;
self.annotations = tmpAnnotations;
[self.mapView reloadInputViews];
});
}
else if ([data length] == 0 && error == nil)
{
NSLog(@"Nothing was downloaded.");
}
else if (error != nil){
NSLog(@"Error = %@", error);
}
}];
}
这是我遇到问题的地方,我想使用来自 json 数据的信息在 UImapview 上显示注释。请查看我在下面这部分代码中遇到的错误,并在它们正在发生的行上进行了评论
if ([data length] >0 && error == nil)
{
NSMutableArray* tmpLocations = [[NSMutableArray alloc] init];
for (NSDictionary* loc in locations) { //receiving error use of undeclared identifier 'locations', did you mean 'Location'
Location* location = [[Location alloc] initWithParameters:loc];
[tmpLocations addObject:location];
}
NSMutableArray* tmpAnnotations;
for (NSDictionary* location in tmpLocations)
{
// retrieve latitude and longitude from location
MKPointAnnotation* annotation = [[MKPointAnnotation alloc] init];
annotation.title = location.address; // receiving error Property 'address' not found on object of type 'NSDictionary'
newAnnotation.coordinate = location; // receiving error use of undeclared identifier 'newAnnotation'
[tmpAnnotations addObject:annotation];
}
dispatch_async(dispatch_get_main_queue(), ^{
self.locations = tmpLocations; /// receiving error Property 'locations' not found on object of type 'MapViewController'
self.annotations = tmpAnnotations; /// receiving error Property 'annotations' not found on object of type 'MapViewController'
[self.mapView reloadInputViews];
});
}
这是我的 MapViewController.h
#import <UIKit/UIKit.h>
#import <MapKit/MapKit.h>
@interface MapViewController : UIViewController <MKMapViewDelegate>
@property (nonatomic, retain) IBOutlet MKMapView *mapView;
- (IBAction)refreshTapped:(id)sender;
@end