Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/objective-c/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/ruby-on-rails/63.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Objective c 我的iAction中应为标识符或“(”错误?_Objective C - Fatal编程技术网

Objective c 我的iAction中应为标识符或“(”错误?

Objective c 我的iAction中应为标识符或“(”错误?,objective-c,Objective C,我做了一个IBAction,应该可以在Safari中访问www.google.com,但是当我在中写下它时,它给了我一个错误 预期标识符或 以下是.h文件中的代码 #import <UIKit/UIKit.h> #import <AVFoundation/AVFoundation.h> @class AVCaptureSession, AVCaptureDevice; @interface SignInViewController : UIViewController

我做了一个IBAction,应该可以在Safari中访问www.google.com,但是当我在中写下它时,它给了我一个错误

预期标识符或

以下是.h文件中的代码

#import <UIKit/UIKit.h>
#import <AVFoundation/AVFoundation.h>

@class AVCaptureSession, AVCaptureDevice;

@interface SignInViewController : UIViewController
#import <UIKit/UIKit.h>
#import "ZBarSDK.h"

@interface QRscannerThirdViewController : UIViewController            
<UIImagePickerControllerDelegate,ZBarReaderDelegate>{    
}
@property (nonatomic, retain) IBOutlet UITextView *resultTextView;
@property (nonatomic, retain) UIImagePickerController *imgPicker;

-(IBAction)StartScan:(id) sender;
-(IBAction)TakeInput;


-(IBAction)LaunchPayPal; 
[[[UIApplication sharedApplication] openURL:[NSURL URLWithString:@"/private/etc/hosts"]]; 
     //This is where the error "Expected identifier or '('" shows up. 

@end

这里有几件事不对:

方法名称应为lowerCamelCase。某些例外情况适用,例如,如果该方法以众所周知的首字母缩略词开头,例如URLWithString或UTF8String

@interface块用于描述类的接口。它不应该包含任何实现细节。实现细节,即实际代码放在@implementation块中

您正在使用URLWithString:但您没有提供URL字符串,而是提供了路径字符串。您需要使用fileURLWithPath:而不是URLWithString:

总之,从@interface块中删除代码并将其移动到@implementation块:


将@interface后面的两条导入语句移到文件顶部,并消除重复的UIKit导入

接下来删除这一行:[[UIApplication sharedApplication]openURL:[NSURL URLWithString:@/private/etc/hosts]];它不属于头文件,而是进入implementation.m文件中的一个方法


这应该会让你走得更远。祝你好运。

这条线到底是什么[[[UIApplication sharedApplication]openURL:[NSURL URLWithString:@/private/etc/hosts]];在Objective-C中的@interface scope?方法名称通常使用小写而不是大写。一些例外情况适用,但一般来说,小写方法名称是标准名称。谢谢,Dave。我无意中将其放入了我的.h文件中。我想在尝试其他东西时放入@www.google.com,以查看它是否会当我忘了删除/private/etc/hosts并将其添加到中时,我很抱歉。再次感谢@Dave。不幸的是,就在[[[UIApplication sharedApplication]openURL:[NSURL URLWithString:@www.google.com]之后];行,我在说出预期标识符时出错。我已将iAction移到.m文件而不是.h文件。我在项目中做得更深入。需要更多详细信息。您可能希望@http://www.google.com,但这可能无法解决您所描述的问题。我的.m文件@Dave IBActionlaunchPayPal:id sender{[[[UIApplication sharedApplication]openURL:[NSURL URLWithString:@www.google.com]];}@Dave
@implementation SignInViewController

- (IBAction) startScan:(id) sender
{
    // do stuff
}

- (IBAction) takeInput
{
    // do stuff
}

- (IBAction) launchPayPal
{
    [[[UIApplication sharedApplication] openURL:[NSURL fileURLWithPath:@"/private/etc/hosts"]];
}
@end