カスタムクラスの学生で作成した配列があります。情報は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;
}
}
}
}
}