我有3个文件:m, cgoth .h, cgoth。M(取自https://github.com/guicocoa/cocoa-oauth)
在主要。我调用(在main方法中):
[GCOAuth URLRequestForPath:@"websites"
HTTPMethod:@"GET"
parameters:nil scheme:@"https"
host:@"blah"
consumerKey:CONSUMER_KEY
consumerSecret:CONSUMER_SECRET
accessToken:accessToken
tokenSecret:tokenSecret];
我得到的错误是,它说没有类方法的选择器URLRequestforPath... .
在我导入到main的GCOAuth.h中。M,是声明:
+ (NSURLRequest *)URLRequestForPath:(NSString *)path
HTTPMethod:(NSString *)HTTPMethod
parameters:(NSDictionary *)parameters
scheme:(NSString *)scheme
host:(NSString *)host
consumerKey:(NSString *)consumerKey
consumerSecret:(NSString *)consumerSecret
accessToken:(NSString *)accessToken
tokenSecret:(NSString *)tokenSecret;
实现在GCOAuth.m中。
我已经试过了:
[[GCOAuth alloc] URLRequestForPath:@"websites"
HTTPMethod:@"GET"
parameters:nil scheme:@"https"
host:@"blah"
consumerKey:CONSUMER_KEY
consumerSecret:CONSUMER_SECRET
accessToken:accessToken
tokenSecret:tokenSecret];
但我只是得到错误:没有可见的@interface声明选择器URLRequestForPath... .
我看不出我做错了什么。如果没有selector的类方法,为什么Xcode的自动完成提供这个方法给我使用?EDIT(这是CGOAuth.m的实现):
+ (NSURLRequest *)URLRequestForPath:(NSString *)path
HTTPMethod:(NSString *)HTTPMethod
parameters:(NSDictionary *)parameters
scheme:(NSString *)scheme
host:(NSString *)host
consumerKey:(NSString *)consumerKey
consumerSecret:(NSString *)consumerSecret
accessToken:(NSString *)accessToken
tokenSecret:(NSString *)tokenSecret {
// check parameters
if (host == nil || path == nil) { return nil; }
// create object
GCOAuth *oauth = [[GCOAuth alloc] initWithConsumerKey:consumerKey
consumerSecret:consumerSecret
accessToken:accessToken
tokenSecret:tokenSecret];
oauth.HTTPMethod = HTTPMethod;
oauth.requestParameters = parameters;
NSString *encodedPath = [path stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
NSString *URLString = [NSString stringWithFormat:@"%@://%@%@", scheme, host, encodedPath];
if ([[HTTPMethod uppercaseString] isEqualToString:@"GET"]) {
// Handle GET
if ([oauth.requestParameters count]) {
NSString *query = [GCOAuth queryStringFromParameters:oauth.requestParameters];
URLString = [NSString stringWithFormat:@"%@?%@", URLString, query];
}
}
oauth.URL = [NSURL URLWithString:URLString];
NSMutableURLRequest *request = [oauth request];
if (![[HTTPMethod uppercaseString] isEqualToString:@"GET"] && [oauth.requestParameters count]) {
// Add the parameters to the request body for non GET requests
NSString *query = [GCOAuth queryStringFromParameters:oauth.requestParameters];
NSData *data = [query dataUsingEncoding:NSUTF8StringEncoding];
NSString *length = [NSString stringWithFormat:@"%lu", (unsigned long)[data length]];
[request setHTTPBody:data];
[request setValue:@"application/x-www-form-urlencoded" forHTTPHeaderField:@"Content-Type"];
[request setValue:length forHTTPHeaderField:@"Content-Length"];
}
// return
return request;
}
这里有一些我尝试过的东西,但没有工作,重新启动Xcode和做一个干净的。
我知道Xcode可以识别这个方法,因为(a)代码完成会给我前面提到的方法的名字,(b)它会出现在侧边栏的层次视图中(在左边)。时间是至关重要的!
main类的CGOAuth.h
导入了吗?检查CGOAuth
类是否存在于项目构建阶段