未使用变量'完成'的警告

时间:2012-04-17 18:48:51

标签: objective-c xcode objective-c-blocks

这个实现文件编译并运行正常,但在块中给出了一个未使用的变量警告。我是块新手,所以不太确定是什么问题。已注释警告所在的行。

有人可以解释一下吗?提前谢谢!

#import "RTViewController.h"
#import <AVFoundation/AVFoundation.h>
#import <AudioToolbox/AudioToolbox.h>

@interface RTViewController () {
  AVAudioPlayer *backgroundAudioPlayer;
  SystemSoundID burnRubberSoundID;
}

@end

@implementation RTViewController
@synthesize car;
@synthesize testDriveButton;
@synthesize backgroundImage;

- (void)viewDidLoad
{
    [super viewDidLoad];
    self.title = @"Road Trip";

  NSURL* backgroundURL = [NSURL fileURLWithPath:[[NSBundle mainBundle]
                                                 pathForResource:@"CarRunning" 
                                                 ofType:@"aif"]];
  backgroundAudioPlayer = [[AVAudioPlayer alloc]
                           initWithContentsOfURL:backgroundURL error:nil];
  backgroundAudioPlayer.numberOfLoops = -1;
  [backgroundAudioPlayer prepareToPlay];

  NSURL* burnRubberURL = [NSURL fileURLWithPath:[[NSBundle mainBundle] pathForResource:@"BurnRubber" ofType:@"aif"]];
  AudioServicesCreateSystemSoundID((__bridge CFURLRef)burnRubberURL, &burnRubberSoundID);

}

- (void)viewDidUnload
{
    [self setCar:nil];
    [self setTestDriveButton:nil];
    [self setBackgroundImage:nil];
    [super viewDidUnload];
    // Release any retained subviews of the main view.
}

- (BOOL)shouldAutorotateToInterfaceOrientation:(UIInterfaceOrientation)interfaceOrientation
{
  //return (interfaceOrientation != UIInterfaceOrientationPortraitUpsideDown);
  return (interfaceOrientation == UIInterfaceOrientationPortrait);
}

- (IBAction)TestDrive:(id)sender {
  AudioServicesPlaySystemSound(burnRubberSoundID);
  [self performSelector:@selector(playCarSound) withObject:self afterDelay:.2];

  CGPoint center = CGPointMake(car.center.x, self.view.frame.origin.y + car.frame.size.height/2);
  [UIView animateWithDuration:3 animations:^ {
    car.center = center;
  }
                   completion:^(BOOL finished) {
                     [self rotate];
                   }];
}

-(void)playCarSound {
  [backgroundAudioPlayer play];
}

- (void)rotate {
  CGAffineTransform transform = CGAffineTransformMakeRotation(M_PI);

  void (^animation) () = ^() {
    car.transform = transform;
  };

  void (^completion) (BOOL) = ^ (BOOL finished) {
    [self returnCar];
  };

  [UIView animateWithDuration:3 animations:animation completion:completion];

}

- (void)returnCar {
  CGPoint center = CGPointMake(car.center.x, self.view.frame.origin.y + self.view.frame.size.height - car.frame.size.height/2);

  void (^animation)() = ^() {
    car.center = center;
  };

  void (^completion)(BOOL) = ^(BOOL finished) {
    [self continueRotation];
  };


  [UIView animateWithDuration:3 animations:animation completion:completion];

}

- (void)continueRotation {
  CGAffineTransform transform = CGAffineTransformMakeRotation(0);

  void (^animation)() = ^() {
    car.transform = transform;
  };

  void (^completion)(BOOL) = ^(BOOL finished) {  //Unused variable 'completion'
    [backgroundAudioPlayer stop];
    [backgroundAudioPlayer prepareToPlay];
  };


  [UIView animateWithDuration:3 animations:animation completion:nil];

}
@end

2 个答案:

答案 0 :(得分:0)

该警告仅表示您已实例化变量但未在代码中的任何位置使用它。

但这是一个警告,你的代码仍然会编译,应该可以正常工作。它只是告诉你,如果它永远不会被使用,就没有理由去实例化。

答案 1 :(得分:0)

问题是您忘记在行中使用变量completion

[UIView animateWithDuration:3 animations:animation completion:nil];
                                                              ^^^
                                                              Here

所以改变这一行:

[UIView animateWithDuration:3 animations:animation completion:completion];

另外,请注意您可以像这样编写代码:

- (void)continueRotation
{
    [UIView animateWithDuration:3 animations:^{
        car.transform = CGAffineTransformMakeRotation(0);
    } completion:^(BOOL finished) {
        [backgroundAudioPlayer stop];
        [backgroundAudioPlayer prepareToPlay];
    }];
}