Xcode接口错误,预期的方法体

时间:2018-08-09 10:29:33

标签: ios objective-c xcode

我已将一台ios项目从一台机器复制到我的机器上,我遇到两个错误

  1. 缺少方法声明的上下文
  2. 预期的方法主体

我检查了搜索路径,一切正常。实现文件还包含方法。

#import <Foundation/Foundation.h>
#import <AFNetworking/AFHTTPSessionManager.h>

@interface HumeApiClient : AFHTTPSessionManager

+(HumeApiClient *)SharedClient;
-(void)AddAccessTokenInHeader;

@end


@implementation HumeApiClient


+(HumeApiClient *)SharedClient
{
    static HumeApiClient *sharedClient = nil;
    static dispatch_once_t oncePredicate;
    dispatch_once(&oncePredicate, ^{
        sharedClient = [[self alloc]initWithBaseURL:[NSURL URLWithString:[[NSUserDefaults standardUserDefaults] objectForKey:@"defaultUrl"]]];

    });
    return sharedClient;
}


-(instancetype)initWithBaseURL:(NSURL *)url
{
    self = [super initWithBaseURL:url];
    if (!self)
    {
        return nil;
    }
    self.responseSerializer = [AFJSONResponseSerializer serializer];
    self.requestSerializer =  [AFJSONRequestSerializer serializer];

    return self;
}


-(void)AddAccessTokenInHeader{

    [self.requestSerializer setValue: [[NSUserDefaults standardUserDefaults] valueForKey:ACCESSTOKEN] forHTTPHeaderField:@"Access_Token"];
}
@end

2 个答案:

答案 0 :(得分:0)

您缺少接口的实现,请参见以下示例:

@interface SampleClass:NSObject
/* method declaration */
- (int)max:(int)num1 andNum2:(int)num2;
@end

@implementation SampleClass

/* method returning the max between two numbers */
- (int)max:(int)num1 andNum2:(int)num2 {

   /* local variable declaration */
   int result;

   if (num1 > num2) {
      result = num1;
   } else {
      result = num2;
   }

   return result; 
}

答案 1 :(得分:0)

根据评论,我知道问题中同时显示了.h和.m文件。 为了正常工作,它们应如下所示:

.h

#import <Foundation/Foundation.h>
#import <AFNetworking/AFHTTPSessionManager.h>

@interface HumeApiClient : AFHTTPSessionManager

+(HumeApiClient *)SharedClient;
-(void)AddAccessTokenInHeader;

@end

.m

#import "HumeApiClient.h"

@interface HumeApiClient()
@end

@implementation HumeApiClient

+(HumeApiClient *)SharedClient {
     [...]
}


-(instancetype)initWithBaseURL:(NSURL *)url {
    [...]
}


-(void)AddAccessTokenInHeader{
    [...]
}
@end

这种方式应该可以工作。

此外,建议您不要大写方法名称。