无法识别的选择器发送到实例 Objective-C
Unrecognized selector sent to instance Objective-C
我收到错误消息:
Terminating app due to uncaught exception
'NSInvalidArgumentException', reason: '-[__NSCFConstantString
subjectType]: unrecognized selector sent to instance
我正在尝试按学生正在学习的学科类型将我的应用程序中的学生分类到数组中。
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];
}
}
我不知道我到底做错了什么,因为它在这个阶段崩溃了:
if ((stud.subjectType & AMStudentSubjectTypeDevelopment) | (stud.subjectType & AMStudentSubjectTypeMath)) {
[techArray addObject:stud];
} else {
[humArray addObject:stud];
}
您正在呼叫
stud.subjectType
在将 studentNames (NSString) 复制到学生数组后的 studentArray 中:
[studentArray addObjectsFromArray:studentNameArray];
NSString 无法识别 subjectType。
techArray 和 humArray (NSArray) 类型更改无法添加对象功能。
NSMutableArray *newtechArray = [techArray mutableCopy];
NSMutableArray *newhumArray = [humarray mutableCopy];
if ((stud.subjectType & AMStudentSubjectTypeDevelopment) | (stud.subjectType & AMStudentSubjectTypeMath)) {
[newtechArray addObject:stud];
} else {
[newhumArray addObject:stud];
}
您填写 studentArray
使用:
[studentArray addObjectsFromArray:studentNameArray];
所以 studentArray
包含 NSString
个实例。然后您尝试使用以下方法处理数组:
for (AMStudent* stud in studentArray){
这 而不是 神奇地将 studentArray
中的 NSString
个实例转换为 AMStudent
个实例。此时您不会收到错误,因为 studentArray
可以包含任何类型的对象,因此编译器只相信您知道自己在做什么,并将对 NSString
的引用放入 stud
.然后你做:
if ((stud.subjectType ...
这需要 stud
引用一个 AMStudent
对象,它不需要,它引用一个(常量)字符串,所以你得到错误:
NSInvalidArgumentException', reason: '-[__NSCFConstantString subjectType]: unrecognized selector sent to instance
无需将学生姓名复制到 studentArray
,您需要创建 AMStudent
的实例并将它们添加到数组中。您是否打算在第一个循环中执行此操作?
HTH
非常感谢您的广泛回答,我明白了我的错误。刚刚添加了一个循环并添加了对象学生。
for (NSInteger numberOfStudents = 0; numberOfStudents < 10; numberOfStudents ++){
AMStudent* student = [[AMStudent alloc] init];
student.name = [studentNameArray objectAtIndex:numberOfStudents];
}
[studentArray addObject:student];
我收到错误消息:
Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString subjectType]: unrecognized selector sent to instance
我正在尝试按学生正在学习的学科类型将我的应用程序中的学生分类到数组中。
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];
}
}
我不知道我到底做错了什么,因为它在这个阶段崩溃了:
if ((stud.subjectType & AMStudentSubjectTypeDevelopment) | (stud.subjectType & AMStudentSubjectTypeMath)) {
[techArray addObject:stud];
} else {
[humArray addObject:stud];
}
您正在呼叫
stud.subjectType
在将 studentNames (NSString) 复制到学生数组后的 studentArray 中:
[studentArray addObjectsFromArray:studentNameArray];
NSString 无法识别 subjectType。
techArray 和 humArray (NSArray) 类型更改无法添加对象功能。
NSMutableArray *newtechArray = [techArray mutableCopy];
NSMutableArray *newhumArray = [humarray mutableCopy];
if ((stud.subjectType & AMStudentSubjectTypeDevelopment) | (stud.subjectType & AMStudentSubjectTypeMath)) {
[newtechArray addObject:stud];
} else {
[newhumArray addObject:stud];
}
您填写 studentArray
使用:
[studentArray addObjectsFromArray:studentNameArray];
所以 studentArray
包含 NSString
个实例。然后您尝试使用以下方法处理数组:
for (AMStudent* stud in studentArray){
这 而不是 神奇地将 studentArray
中的 NSString
个实例转换为 AMStudent
个实例。此时您不会收到错误,因为 studentArray
可以包含任何类型的对象,因此编译器只相信您知道自己在做什么,并将对 NSString
的引用放入 stud
.然后你做:
if ((stud.subjectType ...
这需要 stud
引用一个 AMStudent
对象,它不需要,它引用一个(常量)字符串,所以你得到错误:
NSInvalidArgumentException', reason: '-[__NSCFConstantString subjectType]: unrecognized selector sent to instance
无需将学生姓名复制到 studentArray
,您需要创建 AMStudent
的实例并将它们添加到数组中。您是否打算在第一个循环中执行此操作?
HTH
非常感谢您的广泛回答,我明白了我的错误。刚刚添加了一个循环并添加了对象学生。
for (NSInteger numberOfStudents = 0; numberOfStudents < 10; numberOfStudents ++){
AMStudent* student = [[AMStudent alloc] init];
student.name = [studentNameArray objectAtIndex:numberOfStudents];
}
[studentArray addObject:student];