无法将另一个对象添加到包含不同对象的数组中

时间:2011-06-20 10:56:52

标签: iphone objective-c mapkit

有人请帮忙。

我是一个刚刚创建了一个包含我所有的综合诊所对象的数组的菜鸟。现在我需要将一个用户对象(patientDetail对象)添加到此数组中。但无论我如何修改viewDidLoad方法,一些东西似乎不太正确..我无法填充所有的点..只有当我删除所有处理用户对象的代码然后它的工作.. Some1请看看方法如下并建议?我需要添加在PatientDetail对象中,并将其与其他综合诊所一起填充......

感谢阅读=(

- (void)viewDidLoad {
        [super viewDidLoad];

        _annotation2 = [[NSMutableArray alloc] init];

    CLLocation *userLoc = _mapView.userLocation.location;
        CLLocationCoordinate2D userCoordinate = userLoc.coordinate;

    NSLog(@"user latitude = %f",userCoordinate.latitude);
    NSLog(@"user longitude = %f",userCoordinate.longitude);



   _annotations=[[NSMutableArray alloc] init];
       _listOfPolyClinics = [[NSMutableArray alloc] init];

PatientDetails *patientDetails = [[PatientDatabase database] 
                                  patientDetails:_nric];

for (PolyClinics *polyclinics in [[PatientDatabase database] 
                                  polyClinics]){
    [_listOfPolyClinics addObject:polyclinics];
}

[_listOfPolyClinics addObject:patientDetails];



for (PolyClinics *polyclinics1 in _listOfPolyClinics){
    MyAnnotation* myAnnotation=[[MyAnnotation alloc] init];

    if ([polyclinics1 isKindOfClass:[PatientDetails class]]){ 
        CLLocationCoordinate2D theCoordinate3;
        theCoordinate3.longitude = patientDetails.longitude;
        theCoordinate3.latitude = patientDetails.latitude;

        myAnnotation.coordinate = theCoordinate3;
        myAnnotation.title = _nric;
        myAnnotation.subtitle = [NSString        stringWithFormat:@"%i",patientDetails.category];

    }    
    else{
    CLLocationCoordinate2D theCoordinate;
    theCoordinate.longitude = polyclinics1.longtitude;
    NSLog(@"Halo");
    theCoordinate.latitude = polyclinics1.latitude;
    NSLog(@"bye");


    //myAnnotation.pinColor = MKPinAnnotationColorPurple;
    myAnnotation.coordinate = theCoordinate;
    myAnnotation.title = polyclinics1.name;
    myAnnotation.subtitle = [NSString stringWithFormat:@"%i",polyclinics1.telephone];
    }
    [_mapView addAnnotation:myAnnotation];
    [_annotation2 addObject:myAnnotation];

}

1 个答案:

答案 0 :(得分:0)

因为数组中有不同的类,所以不能使用for (PolyClinics *polyclinics1 in _listOfPolyClinics)迭代数组。请改用id,然后询问对象是什么类,然后在必要时将其强制转换为该类。

尝试将第二个for循环更改为

for (id polyclinics1 in _listOfPolyClinics){
    MyAnnotation* myAnnotation=[[MyAnnotation alloc] init];

    if ([polyclinics1 isKindOfClass:[PatientDetails class]]){ 
        CLLocationCoordinate2D theCoordinate3;
        theCoordinate3.longitude = patientDetails.longitude;
        theCoordinate3.latitude = patientDetails.latitude;

        myAnnotation.coordinate = theCoordinate3;
        myAnnotation.title = _nric;
        myAnnotation.subtitle = [NSString stringWithFormat:@"%i",patientDetails.category];
    } else {
        CLLocationCoordinate2D theCoordinate;
        PolyClinics *polyclinic = (PolyClinics *)polyclinics1;
        theCoordinate.longitude = polyclinic.longtitude;
        NSLog(@"Halo");
        theCoordinate.latitude = polyclinic.latitude;
        NSLog(@"bye");

        //myAnnotation.pinColor = MKPinAnnotationColorPurple;
        myAnnotation.coordinate = theCoordinate;
        myAnnotation.title = polyclinic.name;
        myAnnotation.subtitle = [NSString stringWithFormat:@"%i",polyclinic.telephone];
    }
    [_mapView addAnnotation:myAnnotation];
    [_annotation2 addObject:myAnnotation];
}