Rea*_*omp 3 arrays objective-c nsuserdefaults
在这行代码上获取错误消息:
Team *team = [leagueLocal.teams objectAtIndex:indexpath.row];
Run Code Online (Sandbox Code Playgroud)
错误是"使用未声明的标识符'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];
Run Code Online (Sandbox Code Playgroud)
谢谢您的帮助!如果需要更多信息,或者我可以帮助澄清是否需要,请告诉我.
编辑 - 我viewDidLoad在错误下方的右侧添加了一个NSLog,看看我得到了什么,然后它返回null:NSLog(@"object 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)
Run Code Online (Sandbox Code Playgroud)
或者另一种方式是,如果用户之前已经选择了您要查找的内容,那么您可以使用:
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
Run Code Online (Sandbox Code Playgroud)
希望能帮助到你!