我有一個數組,我創建了一個自定義類的學生。這些信息是從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;
}
}
}
}
}
我不知道在哪裏的學生對象初始化,但我想你一直在修改循環內的同一個對象(學生)。 – Rajiv
我在外面創建並初始化變量,並且在每個學生信息已經通過並且學生被新學生信息替換後,我正在修改學生 – paul590
這是什麼行?:[studentArray addObject:artist];這裏是什麼藝術家? –