没有添加到NSMutable数组的对象

时间:2016-10-16 07:13:36

标签: ios objective-c nsmutablearray nsarray

alter table ACCTMANAGER 
add (Comm_id varchar2(99), 
Ben_id varchar2(99)); 

在上面的程序中,我尝试将student对象添加到StudentCount类的Student.h #import <Foundation/Foundation.h> @interface Student : NSObject @property NSInteger age; @property NSString *name; @end Student.m #import "Student.h" @implementation Student @end StudentCount.h #import <Foundation/Foundation.h> #import "Student.h" NSMutable @interface StudentCount : NSObject @property NSMutableArray<Student *> *student; -(void)addStu:(Student *)stud; -(void)printStudents; @end StudentCount.m #import "StudentCount.h" @implementation StudentCount -(void)addStu:(Student *)stud{ [_student addObject:stud]; } -(void)printStudents{ for(Student *s in _student){ NSLog(@"%li",s.age); NSLog(@"%@",s.name); } } @end Main.m #import <Foundation/Foundation.h> #import "Student.h" #import "StudentCount.h" int main(int argc, const char * argv[]) { @autoreleasepool { Student *student1=[Student alloc]; student1.age=10; student1.name=@"Nirmal"; Student *student2=[Student alloc]; student2.age=12; student2.name=@"Anand"; StudentCount *stCount=[StudentCount alloc]; [stCount addStu:student1]; [stCount addStu:student2]; [stCount printStudents]; } return 0; } 。 之后我尝试调用NSMutableArray类的printStudents方法。 学生对象未添加到StudentCount

上述计划的输出:

  

程序以退出代码结束:0

请告知我哪里出错了。

1 个答案:

答案 0 :(得分:1)

您需要分配NSMutableArray * student

-(void)addStudent:(Student *)stud
{
    if (_student == nil) {
        _student = [[NSMutableArray alloc] init];
    }
    [_student addObject:stud];
}