2011-06-20 24 views
0

有人請幫忙。不能將另一個對象添加到包含不同對象的數組中

我是一個小菜鳥,剛剛創建了一個數組來包含我所有的polyclinics對象。現在我需要添加一個用戶對象(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]; 

} 
+0

什麼是您的控制檯錯誤,並且您能展示您如何實施PatientDetails? – Cyprian

+0

@Cyprian沒有控制檯錯誤..它只是沒有得到填充...或者當我插入這個用戶到地圖..整個地圖將顯示一個世界地圖。,而不是先前縮放的地圖,我可以c我所有的綜合診所......你知道什麼可能是問題嗎? –

回答

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]; 
} 
+0

感謝您的及時答覆..我試過這個,但仍然無法填充這一點..現在地圖不再縮放到我想要的特定區域,但開始與世界地圖,我必須放大點。 ..你知道什麼可能仍然是錯誤嗎? =( –

+0

OMG ..我真的是noob ..對不起你的代碼工作奇蹟...總救生員...謝謝Sascha ...調試,並意識到這是我的SQL語句,給我的問題.. thx百萬! –

相關問題