代码之家  ›  专栏  ›  技术社区  ›  nickthedude

由于NsrangeException nscFarray对象索引:索引(5)…超出界限(5),PickerView应用程序崩溃

  •  1
  • nickthedude  · 技术社区  · 15 年前

    再次开始开发iphone,我正在组装一组uipicker类型的应用程序,最后一个是吃角子老虎机类型的游戏,非常简单。不管怎样,我不太明白为什么会出现这个错误。据我所知,选择器视图正在向其委托请求数组中超出该数组可用范围的对象。

    也就是说,我不知道为什么要这样做,也不知道如何修复它,如果有任何帮助,我将不胜感激,这是来自特定.m文件的代码(请在此处获取完整的项目: http://files.me.com/knyck2/89q3w3 ):

     //
     //  CustomPickerViewController.m
     //  Pickers
     //
     //  Created by Nicholas Iannone on 1/29/10.
     //  Copyright 2010 Apple Inc. All rights reserved.
     //
    
     #import "CustomPickerViewController.h"
    
    
     @implementation CustomPickerViewController
    
     @synthesize column1, column2, column3, column4, column5, picker, winLabel;
    
     -(IBAction) spin : (id) sender {
    
    NSLog(@"even got here");
    
    BOOL win = NO;
    
    int numInRow = 1;
    
    int lastVal = -1;
    
    for (int i = 0; 1 < 5; i++) {
        int newValue = random() % [self.column1 count];
    
        if (newValue == lastVal) {
            NSLog(@"even got here");
    
        numInRow++;
        }
        else 
            numInRow = 1;
    
            lastVal = newValue;
        [picker selectRow:newValue inComponent:i animated:YES];
        [picker reloadComponent:i];
        if (numInRow >= 3) 
            win = YES;
    
        NSLog(@"even got here");
    
    }
    
    if (win) 
        winLabel.text = @"winner!";
    
                  else {
        winLabel.text = @"";
                  NSLog(@"even got here");
                  }
    
     }
    
     /*
      // The designated initializer.  Override if you create the controller programmatically                and want to perform customization that is not appropriate for viewDidLoad.
     - (id)initWithNibName:(NSString *)nibNameOrNil bundle:(NSBundle *)nibBundleOrNil {
    if (self = [super initWithNibName:nibNameOrNil bundle:nibBundleOrNil]) {
        // Custom initialization
    }
    return self;
     }
     */
    
    
     // Implement viewDidLoad to do additional setup after loading the view, typically from      a nib.
     - (void)viewDidLoad {
    UIImage *seven = [UIImage imageNamed:@"seven.png"];
    UIImage *bar = [UIImage imageNamed:@"bar.png"];
    UIImage *crown = [UIImage imageNamed:@"crown.png"];
    UIImage *cherry = [UIImage imageNamed:@"cherry.png"];
    UIImage *lemon = [UIImage imageNamed:@"lemon.png"];
    UIImage *apple = [UIImage imageNamed:@"apple.png"];
    
        for (int i = 1; i <= 5 ;  i++) {
            UIImageView *sevenView = [[UIImageView alloc] initWithImage: seven];
            UIImageView *barView = [[UIImageView alloc] initWithImage: bar];
            UIImageView *crownView = [[UIImageView alloc] initWithImage: crown];
            UIImageView *cherryView = [[UIImageView alloc] initWithImage:      cherry];
            UIImageView *lemonView = [[UIImageView alloc] initWithImage: lemon];
            UIImageView *appleView = [[UIImageView alloc] initWithImage: apple];
    
    
            NSArray *imageViewArray = [[NSArray alloc] initWithObjects:      sevenView, barView, crownView, cherryView, lemonView, appleView, nil];
            NSString *fieldName =[[NSString alloc] initWithFormat:@"column%d",      i];
            [self setValue:imageViewArray forKey:fieldName];
            [fieldName release];
            [imageViewArray release];
    
            [sevenView release];
            [crownView release];
            [barView release];
            [cherryView release];
            [lemonView release];
            [appleView release];
    
        }
    
    srandom(time(NULL));
    [super viewDidLoad];
     }
    
    
    
     /*
     // Override to allow orientations other than the default portrait orientation.
     -(BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation {
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
     }
     */
    
     - (void)didReceiveMemoryWarning {
    // Releases the view if it doesn't have a superview.
    [super didReceiveMemoryWarning];
    
    // Release any cached data, images, etc that aren't in use.
     }
    
     - (void)viewDidUnload {
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
     }
    
    
     - (void)dealloc {
    [picker release];
    [winLabel release];
    [column1 release];
    [column2 release];
    [column3 release];
    [column4 release];
    [column5 release];
    
    
    [super dealloc];
     }
    
    
     #pragma mark -
     #pragma mark Picker Data Source Methods
    
     -(NSInteger) numberOfComponentsInPickerView: (UIPickerView *)  pickerView {
    
    return 5;
     }
    
    
     -(NSInteger) pickerView: (UIPickerView *) pickerView numberOfRowsInComponent:      (NSInteger) component {
    
    return [self.column1 count];
    
     }
    
     #pragma mark Picker Delegate Methods
    
     -(UIView *) pickerView:(UIPickerView *)pickerView viewForRow:(NSInteger)row forComponent: (NSInteger) component reusingView : (UIView *)view {
    
    NSString *arrayName = [[NSString alloc] initWithFormat:@"column%d", component + 1];
    NSArray *array = [self valueForKey:arrayName];
    NSLog(@"got here yo");
    return [array objectAtIndex: row];
    NSLog(@"holyshit");
    
    
     }
    
     @end
    
    2 回复  |  直到 15 年前
        1
  •  3
  •   Malaxeur    15 年前

    它断开的原因是这个循环。看看你的for循环条件:

    1 < 5 . 是的,1是 总是 小于5。这意味着你有一个无限的循环。我相信你的意思是 i < 5 .

    for (int i = 0; 1 < 5; i++) {
        int newValue = random() % [self.column1 count];
    
        if (newValue == lastVal) {
            NSLog(@"even got here");
    
        numInRow++;
        }
        else 
            numInRow = 1;
    
            lastVal = newValue;
        [picker selectRow:newValue inComponent:i animated:YES];
        [picker reloadComponent:i];
        if (numInRow >= 3) 
            win = YES;
    
        NSLog(@"even got here");
    
    }
    
        2
  •  2
  •   Chuck    15 年前

    你在测试如果 1 < 5 ,这总是正确的。你想要 i < 5 .