expected Identifier ] in xcode [closed]

2019-09-25 07:18发布

问题:

I decided to program an iphone app to see how easy it was. I have finalized most of the coding except this part, please help:

#import "ViewController.h"

@interface ViewController ()

@end

@implementation ViewController

- (void)viewDidLoad
{
    [super viewDidLoad];
    //1
    NSString *urlString = @"http://zaphod_beeblebrox.pythonanywhere.com/";
    //2
    NSURL *url = [NSURL URLWithString:urlString];
    //3
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    //4
    NSOperationQueue *queue = [[NSOperationQueue alloc] init];
    //5
    [NSURLConnection sendAsynchronousRequest:request queue:queue
                           completionHandler:^(NSURLResponse *response, NSData *data, NSError *error) {
                               if ([data length] > 0 && error == nil) [UIWebView]
                               else if (error != nil) NSLog(@"Error: %@", error);
                           }];
}

@end

回答1:

the [UIWebView] statement is nonsense/meaningless and not a valid Objective-C syntax.

(I don't even understand what your tried to do there with this statement…)



回答2:

The source of your error message is that you currently have [UIWebView], which makes no sense because the square brackets designate that you're invoking a method, but you have a class name, but you don't have any method name. It's not clear what you're trying to do. See the Object Messaging section of the Objective-C Programming Language, which talks about invoking object methods (i.e. sending the object a message).

I'm guessing you're trying to load a html page in a UIWebView? You obviously need an IBOutlet for your UIWebView. (If that doesn't make sense, I'd suggest you start with Apple's tutorial Your First iOS App.)

In my example below, I'm going to assume your IBOutlet is called webview, and thus I might advise getting rid of the NSOperationQueue and NSUrlConnection and just have the UIWebView load the html for you:

- (void)viewDidLoad
{
    [super viewDidLoad];

    NSString *urlString = @"http://zaphod_beeblebrox.pythonanywhere.com/";
    NSURL *url = [NSURL URLWithString:urlString];
    NSURLRequest *request = [NSURLRequest requestWithURL:url];
    [self.webview loadRequest:request];
}

It might be worth going through a few iPhone programming tutorials (just google it and you'll get tons of hits), too or look at the Apple App Programming Guide or check out the wonderful resources at http://developer.apple.com.