2016-10-16 9 views
-1
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; 
     [email protected]"Nirmal"; 

     Student *student2=[Student alloc]; 
     student2.age=12; 
     [email protected]"Anand"; 

     StudentCount *stCount=[StudentCount alloc]; 
     [stCount addStu:student1]; 
      [stCount addStu:student2]; 

     [stCount printStudents]; 


    } 
    return 0; 
} 

上記のプログラムでは、StudentCountクラスのNSMutableArrayにstudentオブジェクトを追加しようとしました。 その後、私はStudentCountクラスのprintStudentsメソッドを呼び出そうとしました。 生徒オブジェクトはNSMutableArrayに追加されません。上記のプログラムのオブジェクトがNSMutable配列に追加されない

出力:終了コードで終了し

プログラム:私は間違っているつもりです。ここで、0

を教えてください。

+0

student_s_ "student"を含む配列を呼び出さないでください。それを生徒と呼んでください。あなたのコードに横たわって、あなたと他の人たちを混乱させます。 – gnasher729

+0

あなたは各生徒のためのinit呼び出しがありません。 – gnasher729

答えて

1

NSMutableArray * studentを割り当てる必要があります。

+1

ありがとう! @iAiatorそれは働いた。 – Harish

関連する問題