我有一个使用自定义类学生创建的数组。该信息是从一个 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;
}
}
}
}
}