gpt4 book ai didi

ios - 第二次使用一个方法给我一个错误(IOS)

转载 作者:塔克拉玛干 更新时间:2023-11-01 21:36:24 26 4
gpt4 key购买 nike

我正在用 objective c (Xcode) 编写这个应用程序。

当我第一次使用一个方法时一切顺利,但是当我第二次使用它时却出现错误。

我尝试调试它,错误出现在 add tutor in the line if([tutor.userName isEqualToString:userName]) 的方法中

这是错误:

-[__NSCFConstantString userName]: unrecognized selector sent to instance 0xad14c 2016-02-26 20:10:44.043 project[1258:35474] *** Terminating app due to uncaught exception 'NSInvalidArgumentException', reason: '-[__NSCFConstantString userName]: unrecognized selector sent to instance 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];
}

请帮忙!!

谢谢。

最佳答案

这是失败的,因为您在 favTutors 数组中添加了 username 而不是 Tutor 的对象。因此,当您添加第一个对象然后下次通过 self.favTutors 进行枚举时,您实际上有一个字符串对象,它不会响应 username 属性。

关于ios - 第二次使用一个方法给我一个错误(IOS),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/35658935/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com