-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;
                }
            }

        }

    }

}
4

1 回答 1

1

您的问题(除了代码拼写错误)在您的评论中显示:

我在外面创建并初始化了变量,并且在每个学生信息都经过并且用新的学生信息替换学生后我正在修改学生

您只有一个学生对象,您将其重复添加到同一个数组中,每次通过循环时都会更改它的数据。该数组最终包含一系列指向同一个学生对象的指针,该对象仅包含循环中最后一次迭代的数据。

您需要在每个循环上创建并初始化一个新的学生对象。该学生对象将被添加到数组中,然后下一次通过循环,您将创建一个新的学生对象来保存下一个学生的数据。

除此之外,您的代码还包含错误、重复和奇怪的逻辑。尽量减少嵌入的 if/else 子句,这会导致糟糕的编码风格。最好在发布到 SO 之前清理这类事情。

这里:

[studentArray addObject:artist];

我确定你的意思是

[studentArray addObject:student];

我不得不假设给你一个有用的答案......

于 2013-08-29T09:00:57.843 回答