How do I place an __NSCFNumber in an NSString? [closed]
Asked Answered
C

3

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

Cheeseparing answered 24/2, 2014 at 17:29 Comment(6)
what error are you receiving?Sienese
What do you get for post?Hardhearted
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 errorsCheeseparing
it doesn't matter whether its NSNumber or NSCFNumber.check this link for differences #4357563.Kamakura
@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.Hardhearted
Okay, sorry I misread your question, when I call post in NSLog I get "&task_id=XXX". Maybe the problem lies somewhere else then.Cheeseparing
C
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

Cheeseparing answered 24/2, 2014 at 17:44 Comment(0)
T
11
NSNumber *myNumber = @12;
NSString *myString = [myNumber stringValue];
NSString *post = [NSString stringWithFormat:@"&task_id=%@", myString];

It was already answered here

Teishateixeira answered 24/2, 2014 at 17:32 Comment(3)
I already tried that answer.Cheeseparing
Why should that work better than the original code? Btw. [NSNumber stringValue] does not compile.Hardhearted
Where he said NSNumber, it is assumed that you would put in your variable for your NSNumber object.Elisavetpol
K
2

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

NSString *post = [NSString stringWithFormat:@"&task_id=%d", task_id.intValue];
Kamakura answered 24/2, 2014 at 17:30 Comment(5)
Why should that work better than the original code?Hardhearted
well i said, it should work for obvious reasons. But this kinda plan B.Kamakura
"It should work" is a useful comment. I can see no reason why "Plan B" should work better ...Hardhearted
@MartinR care to explain "betterness"? pls . iam a leaner.Kamakura
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.Hardhearted
C
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

Cheeseparing answered 24/2, 2014 at 17:44 Comment(0)

© 2022 - 2024 — McMap. All rights reserved.