【问题标题】:Problem Exc_Arithmetic in Obj-C program for iphoneiphone的Obj-C程序中的Exc_Arithmetic问题
【发布时间】:2011-10-22 21:07:26
【问题描述】:

我正在编写一个带有 UIImageView 的程序。但是,我遇到了错误:Exc_Arithmetic。

这是我的实现:

#import "SalaatAppViewController.h"

@implementation SalaatAppViewController

@synthesize imageView;
@synthesize pauseButton;
@synthesize playButton;
@synthesize nextButton;

-(IBAction)pushPauseButton {
    [imageView stopAnimating];
}

-(IBAction)pushPlayButton {
    [imageView startAnimating];
}

- (IBAction)pushNextButton {

    imagesIndex = (imagesIndex + 1) % [images count];
    imageView.image = [images objectAtIndex:imagesIndex];

}

- (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.
}

#pragma mark - View lifecycle


// Implement viewDidLoad to do additional setup after loading the view, typically from a nib.
- (void)viewDidLoad
{
    imageView.animationImages = [[NSArray arrayWithObjects:
                                 [UIImage imageNamed:@"1.png"],
                                 [UIImage imageNamed:@"2.png"],
                                 [UIImage imageNamed:@"3.png"],
                                  nil] retain];
    UIImage *image = [UIImage imageNamed: @"2.png"];
    [imageView setImage:image];
    imageView.animationDuration = 1.00;
    imageView.animationRepeatCount = 0;
    [imageView startAnimating];
    imagesIndex = 0;
    imageView.image = [images objectAtIndex:imagesIndex];
    [super viewDidLoad];
    [self.view addSubview:imageView];

}


- (void)viewDidUnload
{
    [super viewDidUnload];
    // Release any retained subviews of the main view.
    // e.g. self.myOutlet = nil;
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
    // Return YES for supported orientations
    return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

-(void) dealloc {
    [imageView release];
    [pauseButton release];
    [playButton release];
    [nextButton release];
    [super dealloc];

}

@end

我的标题:

#import <UIKit/UIKit.h>

@interface SalaatAppViewController : UIViewController {
    UIImageView *imageView;
    UIButton *pauseButton;
    UIButton *playButton;
    UIButton *nextButton;
    NSUInteger images;
    NSUInteger imagesIndex;
}

@property (nonatomic, retain) IBOutlet UIImageView *imageView;
@property (nonatomic, retain) IBOutlet UIButton *pauseButton;
@property (nonatomic, retain) IBOutlet UIButton *playButton;
@property (nonatomic, retain) IBOutlet UIButton *nextButton;


-(IBAction)pushPauseButton;
-(IBAction)pushPlayButton;
-(IBAction)pushNextButton;

@end

程序运行,但出现 9 个警告和 1 个错误。另外,当我按下下一个按钮时,它会崩溃。请帮忙!

【问题讨论】:

    标签: iphone ios debugging error-handling


    【解决方案1】:

    异常表明你试图除以 0。这个表达式做了一个模数 (%) 函数,它返回除法后的余数,可能 [images count] 为 0:

    imagesIndex = (imagesIndex + 1) % [images count];
    

    【讨论】:

      【解决方案2】:

      你永远不会初始化images。另外,当您调用count 方法时,它似乎应该是NSArray 而不是NSUInteger

      【讨论】:

      • 当图像是 NSArray 时,我应该使用哪段代码来初始化图像?
      【解决方案3】:

      Ahmad 你的“images”属性不应该是 NSUInteger 而是 NSArray*。定义如下:

      NSArray *images;
      

      按照我之前的指导,您似乎更换了太多东西。 =)

      【讨论】:

      • 是的,我有一个。你想向我展示你的应用程序吗? =)如果你能选择我的答案作为答案,如果它真的对你有帮助,我会很高兴。
      • 我是一个 14 岁的菜鸟,srry
      • i.imgur.com/uqJeW.png — 只需在我的答案下打勾(在这个和上一个主题下,因为它也很有帮助=))
      • 哇。 14岁的你很酷。在你们这个年纪我没有写过 C :(
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2011-07-14
      • 1970-01-01
      • 2011-07-23
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多