【问题标题】:"Incompatible pointer to integer conversion" when using blocks使用块时“指向整数转换的指针不兼容”
【发布时间】:2012-07-26 11:20:08
【问题描述】:

我在下面的代码中收到了这些警告。 任何想法如何解决这个问题? 感谢您的帮助。

  • 缺少类型说明符,默认为 'int'
  • 指向使用“void *”类型表达式初始化“int”的整数转换指针不兼容;
  • 未使用的变量“mymoviePlayerController”

重要的一行是"__block mymoviePlayerController = nil;

- (void) moviePlaybackCompleteLightBox:(NSNotification*) notification {

        MPMoviePlayerController *mymoviePlayerController = [notification object];  
        [[NSNotificationCenter defaultCenter] removeObserver:self  
                                                        name:MPMoviePlayerPlaybackDidFinishNotification  
                                                      object:mymoviePlayerController]; 


        // movie fadein transition ====================
        self.moviePlayerController.view.alpha = 1;

        [UIView animateWithDuration:0.3f delay:0.0 options:UIViewAnimationCurveEaseOut
                         animations:^{
                             self.moviePlayerController.view.alpha = 0;   
                         }
                         completion:^(BOOL finished) { 
                             [mymoviePlayerController stop];
                             [mymoviePlayerController.view removeFromSuperview];
                             __block mymoviePlayerController = nil;

                         }];

    }

【问题讨论】:

  • 您是否也在.h 文件中声明了“mymoviePlayerController”?我的意思是本地和全球??
  • 同时指定您收到第一个和第二个警告的行
  • "重要的一行是 "__block mymoviePlayerController = nil;" 这会引发警告。
  • 如果您使用的是 ARC,则无需编写此行。 [removefromSuperview] 这样做。

标签: iphone objective-c ios objective-c-blocks


【解决方案1】:

__block 在您声明变量时使用,而不是在您为其赋值时使用。所以编译器把下面这行当做变量声明,这是错误的:

 __block mymoviePlayerController = nil; 

声明变量时应该使用__block属性:

__block MPMoviePlayerController *mymoviePlayerController = [notification object];

P.S.你为什么还要在这里使用__block?在这种情况下看起来你不需要它

【讨论】:

    【解决方案2】:

    首先,如果您以后不使用它,您不必将mymoviePlayerController 变量设置为 nil。不用担心,将控制器的视图从其父视图中移除就足够了。

    其次,您不能使用块内的__block 限定符使变量可写。您必须修改代码以使变量在块外可写:

    __block MPMoviePlayerController *blockMoviePlayerController = mymoviePlayerController;
    [UIView animate...animations:...complection:^(BOOL finished) {
        blockMoviePlayerController = nil; // or something else
    }];
    

    【讨论】:

    • 感谢 Vladimir 和 Fabian 的帮助。很高兴知道。它解决了这个问题。感谢您的快速回复。祝你有美好的一天。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-04-14
    相关资源
    最近更新 更多