2012-04-17 69 views
0

有這個實現文件編譯和運行良好,但給我一個塊中未使用的變量警告。對於塊是新的,所以我不太清楚哪裏出了問題。已經評論警告的位置。未使用的變量'完成'的警告

可有人請解釋一下嗎?提前致謝!

#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 

回答

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]; 
    }]; 
} 
+0

這樣做了;非常感謝你! – pdenlinger 2012-04-17 18:59:27

0

該警告僅僅意味着您已經實例化了一個變量,但未在代碼中的任何位置使用它。

但這是一個警告,你的代碼仍然會編譯,應該可以正常工作。它只是告訴你,如果它永遠不會被使用,沒有理由實例化某些東西。