2011-06-20 17 views
0

誰か助けてください。異なるオブジェクトを含む配列に別のオブジェクトを追加することはできません

私はここで誰も私のpolyclinicsオブジェクトをすべて含む配列を作成しています。今度は、この配列にユーザーオブジェクト(patientDetailオブジェクト)を追加する必要があります。しかし、どのように私はviewDidLoadメソッドを変更するにもかかわらず、何かちょうど正しくないようだ..私はすべてのポイントを設定することはできません..私はすべてのコードを削除するときに、ユーザーオブジェクトを扱うそれは動作します.. Some1は以下の方法をお勧めしますか?私はあなたの配列の異なるクラスを持っているので、あなたがfor (PolyClinics *polyclinics1 in _listOfPolyClinics)を使用することはできません読み取り=(

- (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コンソールエラーはありません..それはちょうどそこに住んでいません...またはむしろ私は地図にこのユーザーを挿入..全体の地図は、私ができることができる前の拡大地図ではなく、世界地図を表示します。すべての私のポリクローニクス..あなたは何が問題なのか知っていますか? –

答えて

0

ため

感謝... patientDetailオブジェクトに追加し、クリニックの残りの部分を移入する必要があります。配列を反復処理する代わりにidを使用して、それが何であるか、クラスのオブジェクトを尋ねると、あなたがしなければならない場合、そのクラスにキャストします。

することが

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]; 
} 
にループの2番目を変更するようにしてください
+0

私はこれを試してみましたが、まだその1つのポイントを埋めることができませんでした。そして今、マップはもはや私が望む特定の領域をズームしなくても、ワールドマップで始まり、ポイントをズームする必要があります。 ..まだ何がエラーであるかも知っていますか? =( –

+0

OMG ..私は本当にnoobです。あなたのコードは驚異的に働きました...人生の節約...ありがとうサッシャ...デバッグされ、それは私に問題を与えていた私のSQLステートメントであることに気付いた.. thx ! –

関連する問題