数组崩溃在索引边界?

时间:2012-09-03 08:55:30

标签: objective-c nsarray

任何人都可以帮我解释为什么我的阵列崩溃了。

基本上我有两个按钮可以改变imageView中的图像。

H:

@interface CaseStudySecondPageViewController : UIViewController
{
    //scroller and back and forward buttons for custom control.
    __weak IBOutlet UIScrollView *scroller;
    __weak IBOutlet UIButton *back;
    __weak IBOutlet UIButton *forward;

    //app delegate to return the selected case study from the other controller.
    AppDelegate *del;

    //variables for displaying the case studies
    NSArray *myImageArray;
    NSInteger localSelctorInt;
}

//setup a IBOutlet to allow the image to be changed.
@property (weak, nonatomic) IBOutlet UIImageView *logoImage;

@end

米:

更新方法:

-(void)Updater
{

[logoImage setImage:[myImageArray objectAtIndex:localSelctorInt]];

}

上一个和下一个按钮:

 //returns to previous image if back button is clicked.
-(void) back:(id)sender
{
    if (localSelctorInt > 0)
    {
        localSelctorInt--;
    }
    else
    {
        localSelctorInt = 0;
    }
    [self Updater];
}

//returns to next image if forward button is clicked. increase 7 if array size changes.
//1 removed from int as in starts at 1 and array starts at 0.
-(void) forward:(id)sender
{
    if (localSelctorInt < 7)
    {
        localSelctorInt++;
    }
    else
    {
        localSelctorInt = 7;
    }
    [self Updater];
}

最后我的图像数组在:

中声明
    - (void)viewDidLoad
{
    [super viewDidLoad];

    //setup the delegate to allow access to the int which was modified on the page before.
    del = [[UIApplication sharedApplication] delegate];

    //assign a local variable to the int from the previous page.
    localSelctorInt = *(del.selectorInt);

    //create back and forward buttons as UIButtons first.
    [back addTarget:self action:@selector(back: ) forControlEvents:UIControlEventTouchUpInside];
    [forward addTarget:self action:@selector(forward:) forControlEvents:UIControlEventTouchUpInside];

    //pass the ui buttons into ui bar items.
    UIBarButtonItem *UIBack = [[UIBarButtonItem alloc] initWithCustomView:back];
    UIBarButtonItem *UIForward = [[UIBarButtonItem alloc] initWithCustomView:forward];

    //add these to an array (notice item"s").
    self.navigationItem.rightBarButtonItems = [NSArray arrayWithObjects:UIForward, UIBack, nil];

    //initialize the array with all of the images for the case studies logos.
    myImageArray = [NSArray arrayWithObjects:
            [UIImage imageNamed:@"Logo_Arm.png"],
            [UIImage imageNamed:@"Logo_Fife"],
            [UIImage imageNamed:@"Logo_Findel.png"],
            [UIImage imageNamed:@"Logo_BirkBeck.png"],
            [UIImage imageNamed:@"Logo_NHS_Dudley.png"],
            [UIImage imageNamed:@"Logo_NHS_Kensignton.png"],
            [UIImage imageNamed:@"Logo_Yorkshire_Water.png"],
            [UIImage imageNamed:@"Logo_Uni_Hertfordshire.png"],
            nil];

    //call update method once to load the first image selected from the previous page.
    [self Updater];
}

现在我的应用程序在尝试索引第4张图像(数组中的第3张)时崩溃:

  

*由于未捕获的异常'NSRangeException'而终止应用程序,原因:'* - [__ NSArrayI objectAtIndex:]:索引3超出边界[0 ..   2]“

这可能非常简单,但是感谢你的帮助,欢呼。

2 个答案:

答案 0 :(得分:5)

如果查看异常,则表示您的数组仅包含3个对象(... bounds [0..2] ...)。所以,我的猜测是你的第四张图片不存在...检查[UIImage imageNamed:@"Logo_BirkBeck.png"]的拼写。这个图像真的存在吗?它返回nil,对象列表终止nil,因此,您的数组仅包含3个图像而不是8个图像。

旁注:不要使用像localSelctorInt < 7这样的魔术常量。始终依赖于数组中的实数元素(_myImageArray.count)。这是地狱的一种方式......

第二面注意:不要声明没有下划线前缀的ivars。对于ivar名称,请使用类似_myImageArray的内容。

答案 1 :(得分:1)

我猜它是因为没有分配数组

myImageArray = [[NSArray alloc] initWithObjects:
        [UIImage imageNamed:@"Logo_Arm.png"],
        [UIImage imageNamed:@"Logo_Fife.png"],
        [UIImage imageNamed:@"Logo_Findel.png"],
        [UIImage imageNamed:@"Logo_BirkBeck.png"],
        [UIImage imageNamed:@"Logo_NHS_Dudley.png"],
        [UIImage imageNamed:@"Logo_NHS_Kensignton.png"],
        [UIImage imageNamed:@"Logo_Yorkshire_Water.png"],
        [UIImage imageNamed:@"Logo_Uni_Hertfordshire.png"],
        nil];