如何在AFnetworking中的HTTPHeaderField中设置NSInteger

时间:2015-02-20 06:56:46

标签: ios iphone web-services rest afnetworking-2

我想在HTTPHeaderField中设置NSInteger值。以下是我的代码

NSInteger user_id =1234;
if ([AFNetworkReachabilityManager sharedManager].reachable) {

    AFHTTPRequestOperationManager *afRequestOpManager = [AFHTTPRequestOperationManager manager];
    afRequestOpManager.requestSerializer = [AFJSONRequestSerializer serializer];
    afRequestOpManager.responseSerializer = [AFJSONResponseSerializer serializer];
    [afRequestOpManager.requestSerializer setValue:user_id forHTTPHeaderField:@"user-id"];
    [afRequestOpManager PUT:strUrl parameters:nil success: ^(AFHTTPRequestOperation *operation, id responseObject)
     {
         NSLog(@"JSON: %@", responseObject);
     }failure:^(AFHTTPRequestOperation *operation, NSError *error) {
         NSLog(@"JSON: %ld", error.code);
     }];

但是我无法将user_id设置为整数。

2 个答案:

答案 0 :(得分:0)

传递给setValue的类型是NSString。要将NSInteger转换为NSString,请使用:[NSString stringWithFormat:@"%ld", (long)user_id]

答案 1 :(得分:0)

HTTP标头值只能采用字符串值,不能包含任何其他数据类型值

因此,请将您的用户ID转换为字符串,如下所示

[NSString stringWithFormat:@"%d",user_id]

然后在标题中设置值,也只是为了约定,始终在自定义http标头前加上 X - ,这样你的标题就会变成X-user-id