2013-08-29 14 views
-1

我有一個數組,我創建了一個自定義類的學生。這些信息是從php文件中提取出來的,並在轉換成Json格式後存儲在NSMutableArray中。由於某些原因,當我在數組加載後嘗試拉取學生的名字時,它們都具有相同的名稱。我創建了一個額外的簡單數組,只存儲名稱,並且每當學生數組被填充時該數組被填充,但是簡單數組具有我正在尋找的正確名稱。我包括我的代碼。先謝謝你!!無法創建我需要的信息的數組

代碼:

NSMutableURLRequest *request = [[NSMutableURLRequest alloc]init]; 
[request setURL:[NSURL URLWithString:@"http://mysite.com/students.php"]]; 

NSData *returnData = [NSURLConnection sendSynchronousRequest:request returningResponse:nil error:nil]; //Or async request 
NSError *error=nil; 

NSArray *results = [NSJSONSerialization JSONObjectWithData:returnData options:0 error:&error]; 

//Download information and organize on student class 
for (int i=0; i<[results count];i++) { 
    NSDictionary *DetailDictonary=[results objectAtIndex:i]; 

    if([studentArray count] == 0){ 
     NSString *ID = [DetailDictonary objectForKey:@"ID"]; 
     NSLog(ID); 
     NSString *Name = [DetailDictonary objectForKey:@"post_title"]; 
     NSLog(Name); 

     student.studentID =[DetailDictonary objectForKey:@"ID"]; 

     student.studentName = [DetailDictonary objectForKey:@"post_title"]; 

     student.grade = [DetailDictonary objectForKey:@"post_content"]; 
     [studentArray addObject:artist]; // add to array of students 
     Students *temp = [studentArray objectAtIndex:0]; 
     NSLog(temp.studentName); 
     [tableData addObject:Name]; // simple array used for testing 

    } 

    else{ 
     Students *temp = [studentArray objectAtIndex:0]; 
     NSLog(temp.studentName); // verify if name is still the first name added and has not been replaced 
     for(int i=0; i <[studentArray count]; i++){ 
      tempSt = [studentArray objectAtIndex:i]; 
      NSString *tempcompID = tempSt.studentID; 
      NSString *tempID = [DetailDictonary objectForKey:@"ID"]; 
      NSString *Metakey = [DetailDictonary objectForKey:@"meta_key"]; 
      int ID = [tempID intValue]; 
      int compID = [tempcompID intValue]; 
      NSString *metavalue; 


      ////////////////////////////////////////////// 
      /* BOOL isname = false; 
      for(int i=0; i <[studentArray count]; i++){ 
      Students *temp = [studentArray objectAtIndex:i]; 
      if([temp.studentID isEqualToString:tempID]){ 
      isname = true; 
      } 
      }*/ 
      ////////////////////////////////////////////// 
      if (compID == ID) { 
       NSLog(temp.studentName); 
       NSLog(@"student in the array"); 
       //////////////////////////// 
       NSString *Metakey = [DetailDictonary objectForKey:@"meta_key"]; // get key value onto variable 
       //NSString *Facebook = [[studentArray objectAtIndex:i] getFacebook]; 
       NSString *Facebook = tempSt.facebook; 
       if([Facebook length] == 0 && [Metakey isEqualToString:@"wpzoom_facebook"]){ 

        Facebook = [DetailDictonary objectForKey:@"meta_value"]; 
        student.facebook = Facebook; 


       } 


      } 

      else{ 
       BOOL boolean = false; 
       for(int i=0; i <[studentArray count]; i++){ 
        Students *temp = [studentArray objectAtIndex:i]; 
        if([temp.studentID isEqualToString:tempID]){ 
         boolean = true; 
        } 
       } 
       if(boolean == false){ 
        NSLog(temp.studentName); 
        NSLog(@"student in the array"); 
        NSLog(@"New student create"); 
        NSString *Name = [DetailDictonary objectForKey:@"post_title"]; 
        NSLog(Name); 
        NSString *metavalue; 
        NSLog(@"MADE IT HERE"); 


        student.studentID =[DetailDictonary objectForKey:@"ID"]; 

        student.studentName = [DetailDictonary objectForKey:@"post_title"]; 

        student.bio = [DetailDictonary objectForKey:@"post_content"]; 
        [studentArray addObject:artist]; 

        [tableData addObject:Name]; 
        NSLog(@"IM HERE2"); 
        break; 
       } 
      } 

     } 

    } 

} 
+0

我不知道在哪裏的學生對象初始化,但我想你一直在修改循環內的同一個對象(學生)。 – Rajiv

+0

我在外面創建並初始化變量,並且在每個學生信息已經通過並且學生被新學生信息替換後,我正在修改學生 – paul590

+0

這是什麼行?:[studentArray addObject:artist];這裏是什麼藝術家? –

回答

1

您的問題(除了代碼拼寫錯誤)被發現在您的評論:

我創建和初始化之外的變量,我修改每一個學生後,學生信息已經過並且學生被新學生信息替換

您只有一個您重複添加的學生對象到同一個數組,每次通過循環時更改它的數據。該數組最終包含指向同一個單一學生對象的指針序列,該對象只包含最後一次迭代循環中的數據。

您需要在每個循環中創建並初始化一個新的學生對象。該學生對象將被添加到數組中,然後在下一次循環中,您將創建一個新的學生對象來保存下一個學生的數據。

除了你的代碼包含錯誤,重複和怪異的邏輯。儘量減少嵌入的if/else子句,這會導致編碼風格不佳。在發佈SO之前,最好清理那些東西。

這裏:

[studentArray addObject:artist]; 

我相信你的意思

[studentArray addObject:student]; 

我不得不假設給你一個有用的答案...

+0

這工作!謝謝!是的,我用了一些其他的代碼,並沒有改變這個變量,但感謝注意! – paul590