我在目标c (Xcode)中编写这个应用程序。
当我第一次使用一种方法时,一切都进行得很好,但是当我第二次使用它时,它会给我一个错误。
我试着调试它,错误出现在方法add家教,行if(tutor.userName isEqualToString:userName)
这是一个错误:
-__NSCFConstantString userName:未识别的选择器发送到实例0xad14c 2016-02-26 20:10:44.043项目1258:35474*终止应用程序由于未识别异常'NSInvalidArgumentException',原因:‘__NSCFConstantString userName:未识别的选择器发送到实例0xad14c’
这是我的密码:
- (IBAction)addToFavorite:(id)sender {
AppDelegate *delegate = (AppDelegate *)[[UIApplication sharedApplication] delegate];
NSString * userName = delegate.delegateStr;
Student * student = [[Model instance]getStudentByUserName:userName];
[student addTutor:self.tutor.userName];
UIStoryboard* sb = [UIStoryboard storyboardWithName:@"Main" bundle:nil];
FavoritesTableViewController * ftvc = [sb instantiateViewControllerWithIdentifier:@"favoritesTableViewController"];
ftvc.favTutors = student.favTutors;
[[NSNotificationCenter defaultCenter] postNotificationName:@"RefreshTable" object:nil userInfo:nil];
}
-(void)addTutor:(NSString*)userName {
BOOL check = YES;
for (Tutor * tutor in self.favTutors) {
if([tutor.userName isEqualToString:userName])
check = NO;
}
if(check)
[self.favTutors addObject:userName];
}
救命啊!!
谢谢。
发布于 2016-02-26 10:28:07
这是失败的,因为您在favTutors
数组中添加了favTutors
,而不是Tutor
的对象。因此,当您添加第一个对象,然后下次通过self.favTutors
枚举时,实际上有一个字符串对象,它不会响应username
属性。
发布于 2016-02-26 10:28:04
问题就在这里,我添加了评论。您正在向充满NSString
对象的对象中添加一个Tutor
。在这个方法的第二次调用中,在for循环中,家教实际上是一个NSString
,您试图调用tutor.userName
,并且它不存在于NSString
对象中。
-(void)addTutor:(NSString*)userName {
BOOL check = YES;
for (Tutor * tutor in self.favTutors) {
if([tutor.userName isEqualToString:userName])
check = NO;
}
if(check)
[self.favTutors addObject:userName]; //problem here!!!!
}
我的建议是创建家教对象并将其添加到数组中。
-(void)addTutor:(NSString*)userName {
BOOL check = YES;
for (Tutor * tutor in self.favTutors) {
if([tutor.userName isEqualToString:userName])
check = NO;
}
if(check)
{
// create Tutor object and add it to array dont add NSString directly
}
}
https://stackoverflow.com/questions/35658935
复制