Error :crash my app -[__NSArrayI objectAtIndex:]: index 3 beyond bounds [0 .. 2]'
Error :crash my app -[__NSArrayI objectAtIndex:]: index 3 beyond bounds [0 .. 2]'
我已经创建了一个测验应用程序。但是会产生一些问题,我的问题是 myarray 在我的应用程序崩溃后完成。
我想在打开 UIAlertView 后完成 myarray。怎么可能请帮助。谢谢
// int _currentTitle;
// _currentTitle=0;
// NSArray* myarray;
- (void)viewDidLoad {
[super viewDidLoad];
myarray = [NSArray arrayWithObjects: @"Jill Valentine", @"Peter Griffin", @"Meg Griffin", nil];
}
- (IBAction)changeque:(id)sender {
[self changequestion];
}
-(void)changequestion
{
NSString *str = myarray[_currentTitle++];
questionLabel.text = str;
// if (_currentTitle == myarray.count) { //reload myarray
// _currentTitle = 0;
// }
}
myarray 有 3 个元素,跨越形式
myarray[0] = "Jill Valentine"
,
myarray[1] = "Peter Griffin"
和
myarray[2] = "Meg Griffin"
在 Obj-C 中,您使用 nil 值终止数组,因此 myarray[3] 不包含任何值。但是,当 _currentTitle 达到该值时,您的代码不会显示。
您需要某种条件来检查 _currentTitle 是否等于 [myarray count]。当满足此条件时,调用一个显示警报视图的方法。
示例可以是 -
-(void)changequestion
{
if (_currentTitle >= [myarray count]) {
// call method to present alert view
[self noMoreQuestions];
}
else {
NSString *str = myarray[_currentTitle];
questionLabel.text = str;
}
_currentTitle++; // this is done last so question at index 0 happens
}
-(void)noMoreQuestions {
// present alert to user
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Alert" message:@"Your array count has finished" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
}];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
}];
[alert addAction:ok];
[alert addAction:cancel];
[self presentViewController:alert animated:YES completion:nil];
// remember to reset _currentTitle to 0 before starting next quiz
}
把这个:
-(void)changequestion
{
if (_currentTitle == myarray.count) { //reload myarray
_currentTitle = 0;
}
NSString *str = myarray[_currentTitle++];
questionLabel.text = str;
}
我已经创建了一个测验应用程序。但是会产生一些问题,我的问题是 myarray 在我的应用程序崩溃后完成。
我想在打开 UIAlertView 后完成 myarray。怎么可能请帮助。谢谢
// int _currentTitle;
// _currentTitle=0;
// NSArray* myarray;
- (void)viewDidLoad {
[super viewDidLoad];
myarray = [NSArray arrayWithObjects: @"Jill Valentine", @"Peter Griffin", @"Meg Griffin", nil];
}
- (IBAction)changeque:(id)sender {
[self changequestion];
}
-(void)changequestion
{
NSString *str = myarray[_currentTitle++];
questionLabel.text = str;
// if (_currentTitle == myarray.count) { //reload myarray
// _currentTitle = 0;
// }
}
myarray 有 3 个元素,跨越形式
myarray[0] = "Jill Valentine"
,
myarray[1] = "Peter Griffin"
和
myarray[2] = "Meg Griffin"
在 Obj-C 中,您使用 nil 值终止数组,因此 myarray[3] 不包含任何值。但是,当 _currentTitle 达到该值时,您的代码不会显示。
您需要某种条件来检查 _currentTitle 是否等于 [myarray count]。当满足此条件时,调用一个显示警报视图的方法。
示例可以是 -
-(void)changequestion
{
if (_currentTitle >= [myarray count]) {
// call method to present alert view
[self noMoreQuestions];
}
else {
NSString *str = myarray[_currentTitle];
questionLabel.text = str;
}
_currentTitle++; // this is done last so question at index 0 happens
}
-(void)noMoreQuestions {
// present alert to user
UIAlertController *alert = [UIAlertController alertControllerWithTitle:@"Alert" message:@"Your array count has finished" preferredStyle:UIAlertControllerStyleAlert];
UIAlertAction *ok = [UIAlertAction actionWithTitle:@"OK" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
}];
UIAlertAction *cancel = [UIAlertAction actionWithTitle:@"Cancel" style:UIAlertActionStyleDefault handler:^(UIAlertAction * action) {
}];
[alert addAction:ok];
[alert addAction:cancel];
[self presentViewController:alert animated:YES completion:nil];
// remember to reset _currentTitle to 0 before starting next quiz
}
把这个:
-(void)changequestion
{
if (_currentTitle == myarray.count) { //reload myarray
_currentTitle = 0;
}
NSString *str = myarray[_currentTitle++];
questionLabel.text = str;
}