我有3个文件:main.m,CGOAuth.h,CGOAuth.m(取自https://github.com/guicocoa/cocoa-oauth)
在main.m中,我调用(在主方法中):
[GCOAuth URLRequestForPath:@"websites"
HTTPMethod:@"GET"
parameters:nil scheme:@"https"
host:@"blah"
consumerKey:CONSUMER_KEY
consumerSecret:CONSUMER_SECRET
accessToken:accessToken
tokenSecret:tokenSecret];
我得到的错误是它表示选择器URLRequestforPath没有类方法....
在GCOAuth.h中,我导入main.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 ....
我看不出我做错了什么。如果选择器没有类方法,为什么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)它出现在侧栏(左侧)的分层视图中。 时间至关重要!
答案 0 :(得分:0)
您是否在主要课程中导入了CGOAuth.h
?检查项目构建阶段是否存在CGOAuth
类。