XCode - '预期表达'错误

时间:2015-01-19 18:14:27

标签: objective-c

我对IOS应用创建游戏完全陌生,所以请保持温和。我在autorelease部分得到预期的表达式错误。如果有人可以帮助我,那就太好了。

// Load images
NSArray *imageNames = @[@"fold1.png", @"fold2.png", @"fold3.png", @"fold4.png",
                        @"fold5.png", @"fold6.png", @"fold7.png", @"fold8.png",
                        @"fold9.png", @"fold10.png", @"fold11.png", @"fold12.png",
                        @"foldclear.png"];

NSMutableArray *images = [[NSMutableArray alloc] init];
for (int i = 0; i < imageNames.count; i++) {
    [images addObject:[UIImage imageNamed:[imageNames objectAtIndex:i]]];
}

// Normal Animation
UIImageView *animationImageView = [[UIImageView alloc] initWithFrame:CGRectMake(0, 108, 324, 460)];
animationImageView.animationImages = images;
animationImageView.animationDuration = 0.5;
animationImageView.animationRepeatCount = 5;

[self.view addSubview:animationImageView];
[animationImageView startAnimating];
// Add swipeGestures
UISwipeGestureRecognizer *oneFingerSwipeLeft = [[[UISwipeGestureRecognizer alloc]
                                                 initWithTarget:self
                                                 action:@selector(oneFingerSwipeLeft:) autorelease:];
[oneFingerSwipeLeft setDirection:UISwipeGestureRecognizerDirectionLeft];
[[self view] addGestureRecognizer:oneFingerSwipeLeft];

UISwipeGestureRecognizer *oneFingerSwipeRight = [[[UISwipeGestureRecognizer alloc]
                                                  initWithTarget:self
                                                  action:@selector(oneFingerSwipeRight:) autorelease:];
[oneFingerSwipeRight setDirection:UISwipeGestureRecognizerDirectionRight];
[[self view] addGestureRecognizer:oneFingerSwipeRight];

}

1 个答案:

答案 0 :(得分:1)

对于ARC(自动引用计数)项目,您根本不应该使用autorelease。系统将确定何时为您释放对象。但是,您不必使用ARC。


autorelease方法不接受任何参数。简称:

[object autorelease];

冒号表示预期参数。

[object initWithTarget:self action:@selector(oneFingerSwipeLeft:)];

在这里,您已将self作为第一个参数,@selector(oneFingerSwipeLieft:)作为第二个参数。您已指出oneFingerSwipeLeft:需要在:内使用@selector()将一个参数传递给它。


修改:此外,您似乎没有正确计算该线路上正在调用的方法数。您应该能够单独提取每个[]作为方法。我将采取第一行并为您分开,以便您可以看到:

UISwipeGestureRecognizer *oneFingerSwipeLeft = [[[UISwipeGestureRecognizer alloc]
                                               initWithTarget:self
                                               action:@selector(oneFingerSwipeLeft:) autorelease:];

变为:

UISwipeGestureRecognizer *oneFingerSwipeLeft = [UISwipeGestureRecognizer alloc];
oneFingerSwipeLeft = [oneFingerSwipeLeft initWithTarget:self
                                                 action:@selector(oneFingerSwipeLeft:)
                                            autorelease];

但是,这表明在initWithTarget:action:autorelease上有一个签名为UISwipeGestureRecognizer的方法。没有一个,也不是有效的Objective-C来创建一个。

相反,您应该将initWithTarget:action:用作一个选择器,将autorelease用作自己的选择器:

UISwipeGestureRecognizer *oneFingerSwipeLeft = [UISwipeGestureRecognizer alloc];
oneFingerSwipeLeft = [oneFingerSwipeLeft initWithTarget:self
                                                 action:@selector(oneFingerSwipeLeft:)];
oneFingerSwipeLeft = [oneFingerSwipeLeft autorelease];

将它们放在一行中看起来像:

UISwipeGestureRecognizer *oneFingerSwipeLeft = [[[UISwipeGestureRecognizer alloc]
                                               initWithTarget:self
                                               action:@selector(oneFingerSwipeLeft:)] autorelease];

基本上,你错过了]