5

I have an NSNumber variable called task_id. I want to place that NSNumber variable in an NSString (post request). Here is what I have tried:

NSString *post = [NSString stringWithFormat:@"&task_id=%@", task_id];

and:

NSString *post = [NSString stringWithFormat:@"&task_id=%@", [NSString stringWithFormat:@"%@",task_id]];

For some reason the string doesn't include the task_id value to the POST request. How can I place the NSNumber into the string?

Update

task_id is an NSCFNumber according to this code:

NSLog(@"%@", [task_id class]);

Many thanks indeed,

Peter

Peter Stuart
  • 2,362
  • 7
  • 42
  • 73
  • what error are you receiving? – Kevin DiTraglia Feb 24 '14 at 17:34
  • What do you get for `post`? – Martin R Feb 24 '14 at 17:35
  • I get an empty response, which is expected. When I place a number in the string (hard-coded) I get the appropiate response. I get no objective-c errors – Peter Stuart Feb 24 '14 at 17:37
  • it doesn't matter whether its NSNumber or NSCFNumber.check this link for differences http://stackoverflow.com/questions/4357063/what-is-the-class-nscfnumber-in-ios-4-1. – santhu Feb 24 '14 at 17:40
  • @PeterStuart: I did not ask for the response. If you want help to find the actual problem, then show the NSLog output of `task_id` and `post`, and also the code how you sent the request. – Martin R Feb 24 '14 at 17:40
  • Okay, sorry I misread your question, when I call post in NSLog I get "&task_id=XXX". Maybe the problem lies somewhere else then. – Peter Stuart Feb 24 '14 at 17:43

3 Answers3

11
NSNumber *myNumber = @12;
NSString *myString = [myNumber stringValue];
NSString *post = [NSString stringWithFormat:@"&task_id=%@", myString];

It was already answered here

Community
  • 1
  • 1
zaheer
  • 893
  • 13
  • 27
2

it should work.But anyway, if task-id is just an int ,

NSString *post = [NSString stringWithFormat:@"&task_id=%d", task_id.intValue];
santhu
  • 4,796
  • 1
  • 21
  • 29
  • 1
    Why should that work better than the original code? – Martin R Feb 24 '14 at 17:39
  • well i said, it should work for obvious reasons. But this kinda plan B. – santhu Feb 24 '14 at 17:40
  • 1
    "It should work" is a useful *comment*. I can see no reason why "Plan B" should work better ... – Martin R Feb 24 '14 at 17:44
  • @MartinR care to explain "betterness"? pls . iam a leaner. – santhu Feb 24 '14 at 17:47
  • What I meant is: If `NSString *post = [NSString stringWithFormat:@"&task_id=%d", task_id.intValue];` works then the original code would work as well. There is no reason to replace the original code with the one that you suggested. – Martin R Feb 24 '14 at 17:49
1

Thanks for everyones efforts. It turns out that wasn't the problem, and the problems lies within the web server!

I misunderstood the whole thing,

Thanks guys,

Peter

Shaik Riyaz
  • 11,204
  • 7
  • 53
  • 70
Peter Stuart
  • 2,362
  • 7
  • 42
  • 73