【问题标题】:variable is not assignable (missing __block type specifier)变量不可赋值(缺少 __block 类型说明符)
【发布时间】:2016-04-13 22:03:21
【问题描述】:

我的代码中有一个错误,我无法修复.. 这是我的代码..它在第一个 if 语句中,current_region++ 出现...请帮助我,谢谢

-(void)planetRotation:(UIView *)planet average:(float )time1 perihelion:(float )time2 aphelion:(float )time3 region:(int ) current_region
{
    current_region = 0;

    [planet.layer removeAllAnimations];

    if (current_region == 0 || current_region == 2)
    {
        [UIView animateWithDuration:time1 delay:0.0 options: UIViewAnimationOptionTransitionNone
        animations: ^{ CABasicAnimation* rotationAnimation;
                       rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
                       rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 /* full rotation*/ * time1 * 1 ];//multiply more to add speed
                       rotationAnimation.duration = time1/4;
                       rotationAnimation.cumulative = YES;
                       rotationAnimation.repeatCount =  HUGE_VALF;
            [planet.layer addAnimation:rotationAnimation forKey:@"orbit"];} completion:^(BOOL finished){current_region++; }];
    }
    else if(current_region == 1)
    {
        [UIView animateWithDuration:time2 delay:0.0 options: UIViewAnimationOptionTransitionNone
        animations:^{ CABasicAnimation* rotationAnimation;
                       rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
                        rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 /* full rotation*/ * time2 * 1 ];//multiply more to add speed
                        rotationAnimation.duration = 15;
                        rotationAnimation.cumulative = YES;
                        rotationAnimation.repeatCount =  HUGE_VALF;
            [planet.layer addAnimation:rotationAnimation forKey:@"orbit"];} completion:^(BOOL finished){ }];
    }
    else if(current_region == 3)
    {
        [UIView animateWithDuration:time3 delay:0.0 options: UIViewAnimationOptionTransitionNone
            animations:^{ CABasicAnimation* rotationAnimation;
                        rotationAnimation = [CABasicAnimation animationWithKeyPath:@"transform.rotation.z"];
                        rotationAnimation.toValue = [NSNumber numberWithFloat: M_PI * 2.0 /* full rotation*/ * time3 * 1 ];//multiply more to add speed
                        rotationAnimation.duration = 15;
                        rotationAnimation.cumulative = YES;
                        rotationAnimation.repeatCount =  HUGE_VALF;
            [planet.layer addAnimation:rotationAnimation forKey:@"orbit"];} completion:^(BOOL finished){ }];
    }

【问题讨论】:

    标签: objective-c


    【解决方案1】:

    当您声明 current_region 变量时,您需要像这样修改声明:

    __block int current_region = 0;
    

    Apple says:

    __block 变量存在于变量的词法范围和声明的所有块和块副本之间共享的存储中,或 在变量的词法范围内创建。因此,存储将 如果有任何块的副本,则在堆栈帧的破坏中幸存下来 在框架内声明的存活超出框架的末尾(对于 例如,通过在某处排队以供以后执行)。多 给定词法范围内的块可以同时使用共享 变量。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2015-05-02
      • 1970-01-01
      • 1970-01-01
      • 2012-07-05
      相关资源
      最近更新 更多