objective-c – “objectAtIndex”的NSUserDefaults错误
发布时间:2020-12-16 09:42:49 所属栏目:百科 来源:网络整理
导读:在这行代码上获取错误消息: Team *team = [leagueLocal.teams objectAtIndex:indexpath.row]; 错误是“使用未声明的标识符’indexpath’”…但我不确定我会在objectAtIndex上放什么:除了indexpath.row,因为这对我来说在这个应用程序的TableView部分中有用.
在这行代码上获取错误消息:
Team *team = [leagueLocal.teams objectAtIndex:indexpath.row]; 错误是“使用未声明的标识符’indexpath’”…但我不确定我会在objectAtIndex上放什么:除了indexpath.row,因为这对我来说在这个应用程序的TableView部分中有用. 大图,我正在尝试使用NSUserDefaults保存所选的团队名称.团队名称通过JSON从Web服务中提取. 这是我到目前为止NSUserDefaults部分的完整代码: // Create User Defaults Variable NSUserDefaults *defaults = [NSUserDefaults standardUserDefaults]; // Tell it where to look Sport *sportLocal = [sports objectAtIndex:0]; League *leagueLocal = [sportLocal.leagues objectAtIndex:0]; Team *team = [leagueLocal.teams objectAtIndex:indexpath.row]; // This is where error is // Store the data,this is the stuff I want to save [defaults setObject:team.abbreviation forKey:[NSString stringWithFormat:@"teamAbbreviation%d",_buttonNumber]]; // After saving,synchronize data [defaults synchronize]; 谢谢您的帮助!如果需要更多信息,或者我可以帮助澄清是否需要,请告诉我. 编辑 – 我在错误下方的viewDidLoad中添加了一个NSLog,看看我得到了什么,然后它返回null:NSLog(@“index at index:%@”,team.abbreviation); 解决方法
如果这是在viewDidLoad中,那么除非你初始化了indexPath变量,否则它将无效.它只在tableView方法中有效,因为它是通过函数传入的.
你有几个选项来获取你想要的indexPath: Team *team = [leagueLocal.teams objectAtIndex:[NSIndexPath indexPathForRow:i inSection:k]; //where i and k are integers that indicate the row/section for the tableViewCell your looking for...(k would just be equal to 0 if you only have one section) 或者另一种方式是,如果用户之前已经选择了您要查找的内容,那么您可以使用: NSIndexPath *indexPath=[self.tableView indexPathForSelectedRow]; Team *team = [leagueLocal.teams objectAtIndex:indexPath.row];//now you can use it the way you have it written in your question 希望能帮助到你! (编辑:李大同) 【声明】本站内容均来自网络,其相关言论仅代表作者个人观点,不代表本站立场。若无意侵犯到您的权利,请及时与联系站长删除相关内容! |