【发布时间】:2013-06-13 20:43:21
【问题描述】:
我有 3 个文件:main.m、CGOAuth.h、CGOAuth.m(取自 https://github.com/guicocoa/cocoa-oauth)
在 main.m 中我调用(在 main 方法中):
[GCOAuth URLRequestForPath:@"websites"
HTTPMethod:@"GET"
parameters:nil scheme:@"https"
host:@"blah"
consumerKey:CONSUMER_KEY
consumerSecret:CONSUMER_SECRET
accessToken:accessToken
tokenSecret:tokenSecret];
我得到的错误是它说选择器 URLRequestforPath 没有类方法...。
在我导入 main.m 的 GCOAuth.h 中是声明:
+ (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... 。
我看不出我做错了什么。如果没有选择器的类方法,为什么 Xcode 的自动补全提供方法供我使用?
编辑(这是来自 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) 它出现在侧边栏(左侧)的分层视图中。 时间至关重要!
【问题讨论】:
-
可能是拼写错误?尝试再次将其写入 xcode 编辑器。
-
你能把方法实现显示到左括号吗?
-
@Chuck 直到左括号是什么意思?
-
应该与声明匹配的部分。例如,“+ (void)someMethod:(id)arg1 otherThing:(int)arg2 {”。
-
@Chuck 完成。已添加。
标签: objective-c debugging compiler-errors selector class-method