我正在开发一个网络浏览器应用程序,但目前我遇到了错误:“预期的标识符或'('”并且我不知道该怎么做。以下是显示错误的代码:
{:here is the error
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:urlRequest];
}
非常感谢帮助。
更新:
我不知道我应该给你的其他代码,所以这里是ViewController.m文件中的整个代码:
#import "ViewController.h"
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIWebView *webView;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *back;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *refresh;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *stop;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *forward;
- (void)loadRequestFromString:(NSString*)urlString;
{ *HERE IS THE ERROR:EXPECTED IDENTIFIER OR '('*
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:urlRequest];
}
@end
@implementation ViewController ***HERE IS A WARNING SAYING:METHOD DEFINITION FOR 'LOADREQUESTFROMSTRING:'NOT FOUND***
- (void)viewDidLoad
{
[super viewDidLoad];
// Do any `enter code here`additional setup after loading the view, typically from a nib.
[self loadRequestFromString:@"http://www.apple.com/startpage/"];
}
- (void)didReceiveMemoryWarning
{
[super didReceiveMemoryWarning];
// Dispose of any resources that can be recreated.
}
@end
答案 0 :(得分:1)
为什么在界面中有实现代码
@interface ViewController ()
@property (weak, nonatomic) IBOutlet UIWebView *webView;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *back;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *refresh;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *stop;
@property (weak, nonatomic) IBOutlet UIBarButtonItem *forward;
// This is a private interface and doesn't need the below line ever.
// If you want this public then add it to the interface in the .h file.
- (void)loadRequestFromString:(NSString*)urlString;
*****************************************************************************
// This is implementation code and shouldn't be here.
// This belongs in the implementation not the interface
{ *HERE IS THE ERROR:EXPECTED IDENTIFIER OR '('*
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:urlRequest];
}
*****************************************************************************
@end
在您的实施中@implementation
添加
@implementation ViewController
// Your other code such as viewDidLoad etc
// Adding this method to the implementation will also get rid of the warning
// ***HERE IS A WARNING SAYING:METHOD DEFINITION FOR 'LOADREQUESTFROMSTRING:'NOT FOUND***
// As it will now be implemented but there is no reason to declare the method in a private interface
- (void)loadRequestFromString:(NSString*)urlString;
{
NSURL *url = [NSURL URLWithString:urlString];
NSURLRequest *urlRequest = [NSURLRequest requestWithURL:url];
[self.webView loadRequest:urlRequest];
}
由于这是实现代码,因此它不应存在于接口中。