-1

我正在尝试制作一个简单的 http 请求方法,但是在编译时出现此错误:

 Compiling file main.m ...
In file included from main.m:1:
./HttpManager.h:4:2: error: expected selector for Objective-C method
-<NSURL*> getUrlContent:(NSString) url;
~^
main.m:11:17: warning: instance method '-getUrlContent:' not found (return type defaults to 'id')
        NSURL *myURL = [pHttpManager getUrlContent:@"http://www.cnn.com"];

我的简单源代码:

HttpManager.h

#import <Foundation/Foundation.h>

@interface HttpManager:NSObject
-<NSURL*> getUrlContent:(NSString) url;
@end

HttpManager.m

#import "HttpManager.h"

@implementation HttpManager


-<NSURL*> getUrlContent:(NSString) url
{
    NSURL *myURL = [NSURL URLWithString:url];
    return myURL;
}
#end

主文件

#import "HttpManager.h"
#import <Foundation/Foundation.h>
int main(int argc,char** argv)
{

    HttpManager* pHttpManager;
    pHttpManager  = [[HttpManager alloc] init];
    NSURL *myURL = [pHttpManager getUrlContent:@"http://www.example_site.com"]; 
    NSString *myHomePage = [NSString stringWithContentsOfURL: myURL
                            encoding: NSASCIIStringEncoding error: NULL];

    NSLog(@"%@", myHomePage);

    return 0;
}

我在这里做错了什么?

4

1 回答 1

1

试试这个

-(NSURL*) getUrlContent:(NSString*) url
{
NSURL *myURL = [NSURL URLWithString:url];
return myURL;
}

我们是在使用 <> 而不是 () 吗?也更换它们。

于 2013-01-10T08:43:42.137 回答