Expected Identifier异常

下面的属性声明了一个单例:

@property (nonatomic, copy) void (^onUpdate)();

类中开始这个单例之后:

[self.singleton.onUpdate = ^{ 
  CGAffineTransform rot = CGAffineTransformMakeRotation(degreesToRadian(angle));
  [UIView animateWithDuration:0.1 animations:^{
      myself.object.transform = rot;
  }];
}];

在最后一行报出expected identifier错误。

ChatGPT尝试为您解答,仅供参考
这个错误是因为在给 onUpdate 赋值的时候出现了语法错误。

应该在赋值之后再调用它,就像这样:

self.singleton.onUpdate = ^{ 
  CGAffineTransform rot = CGAffineTransformMakeRotation(degreesToRadian(angle));
  [UIView animateWithDuration:0.1 animations:^{
      myself.object.transform = rot;
  }];
};
self.singleton.onUpdate();

这样就能够正确地调用 onUpdate 了。

另外,建议将这段代码放在一个方法中,这样就可以方便地调用了。例如:

- (void)update {
  CGAffineTransform rot = CGAffineTransformMakeRotation(degreesToRadian(angle));
  [UIView animateWithDuration:0.1 animations:^{
      myself.object.transform = rot;
  }];
}

self.singleton.onUpdate = ^{ [self update]; };
self.singleton.onUpdate();