【发布时间】:2016-02-26 18:18:45
【问题描述】:
我正在用 Objective c (Xcode) 编写这个应用程序。
当我第一次使用一个方法时一切顺利,但是当我第二次使用它时,它给了我一个错误。
我尝试调试它,错误出现在 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];
}
请帮忙!!
谢谢。
【问题讨论】:
-
错误信息说
Tutor实例实际上是NSString,favTutors是如何声明的? -
@property NSMutableArray * favTutors;
-
查看答案,您在
addTutor方法中将NSString对象而不是Tutor对象添加到favTutors。将favTutors声明为轻量级泛型,然后编译器将帮助您。
标签: ios objective-c