1

I am receiving error:

Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString subjectType]: unrecognized selector sent to instance

I am trying to sort students in my app to arrays by the subject type that they are learning.

AMStudent* student = [[AMStudent alloc] init];

NSMutableArray* studentArray = [[NSMutableArray alloc] init];

NSArray* studentNameArray = [NSArray arrayWithObjects: @"Student1",  @"Student2", @"Student3", @"Student4", @"Student5", @"Student6", @"Student7", @"Student8", @"Student9", @"Student10", nil];

[studentArray addObjectsFromArray:studentNameArray];

for (NSInteger i = 0; i < [studentNameArray count]; i++) {

    student.name = [studentNameArray objectAtIndex: i];
    [student randomAnswer];
    NSLog(@"%@", student.description);

}

NSMutableArray* techArray = [NSMutableArray array];
NSMutableArray* humArray = [NSMutableArray array];

for (AMStudent* stud in studentArray){

    if ((stud.subjectType & AMStudentSubjectTypeDevelopment) | (stud.subjectType & AMStudentSubjectTypeMath)) {
        [techArray addObject:stud];
    } else {
        [humArray addObject:stud];
    }
}

I cant figure out what exactly I am doing wrong, because it crashes in this stage:

if ((stud.subjectType & AMStudentSubjectTypeDevelopment) | (stud.subjectType & AMStudentSubjectTypeMath)) {
        [techArray addObject:stud];
    } else {
        [humArray addObject:stud];
    } 
Ronak Chaniyara
  • 5,335
  • 3
  • 24
  • 51

4 Answers4

4

You are calling

stud.subjectType 

in the studentArray after copying the studentNames (NSString) to the student array:

[studentArray addObjectsFromArray:studentNameArray];

NSString won't recognize subjectType.

bc888
  • 141
  • 14
3

You fill studentArray using:

[studentArray addObjectsFromArray:studentNameArray];

So studentArray contains NSString instances. You then attempt to process the array using:

for (AMStudent* stud in studentArray){

This does not magically convert the NSString instances in studentArray into AMStudent instances. You don't get an error at this point as studentArray can contain objects of any type so the compiler just trusts you know what you are doing and places a reference to an NSString into stud. You then do:

   if ((stud.subjectType ...

and this requires stud to reference an AMStudent object, which it does not, it references a (constant) string and so you get the error:

NSInvalidArgumentException', reason: '-[__NSCFConstantString subjectType]: unrecognized selector sent to instance

Instead of copying the names of the students into studentArray you need to create instances of AMStudent and add those to the array. Did you intend to do that in the first loop maybe?

HTH

CRD
  • 52,522
  • 5
  • 70
  • 86
0

techArray and humArray (NSArray) type change not working add object function.

NSMutableArray *newtechArray = [techArray mutableCopy];


NSMutableArray *newhumArray = [humarray mutableCopy];

if ((stud.subjectType & AMStudentSubjectTypeDevelopment) | (stud.subjectType & AMStudentSubjectTypeMath)) {
        [newtechArray addObject:stud];
    } else {
        [newhumArray addObject:stud];
    } 
memedina
  • 26
  • 3
0

Thanks a lot for Your wide answer, I understood my mistake. Just added one more loop and added object student.

for (NSInteger numberOfStudents = 0; numberOfStudents < 10; numberOfStudents ++){

    AMStudent* student = [[AMStudent alloc] init];
    student.name = [studentNameArray objectAtIndex:numberOfStudents];

}

[studentArray addObject:student];