我有一个我用自定义类学生创建的数组。在转换它所引入的Json格式之后,信息从php文件中提取并存储在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 :(得分:1)
您的问题(除了代码拼写错误)在您的评论中显示:
我在外面创建并初始化变量,并且在每个学生信息通过并且学生被新学生信息替换后我正在修改学生
您只有一个学生对象可以重复添加到同一个数组中,每次通过循环时都会更改它的数据。该数组最终包含一个指向同一个学生对象的指针序列,该对象仅包含上一次迭代到循环的数据。
您需要在每个循环上创建并初始化 new student对象。该学生对象将被添加到数组中,然后下一次循环,您将创建一个新的学生对象来保存下一个学生的数据。
除此之外,您的代码包含错误,重复和奇怪的逻辑。尝试减少嵌入的if / else子句,这会导致编码风格不佳。在发布SO之前最好清理那种东西。
下面:
[studentArray addObject:artist];
我相信你的意思
[studentArray addObject:student];
我不得不假设给你一个有用的答案......