0

I am trying to convert two values to string and then make a new string containing the ones i made earlier so my service can accept them.

NSString *string1 = [ NSString stringWithFormat:@"%f", locationController.dblLatitude];
NSString *string2 = [ NSString stringWithFormat:@"%f", locationController.dblLongitude];



body = [[NSString stringWithFormat:@"geoX#%@#geoY#%@", string1, string2] dataUsingEncoding:NSUTF8StringEncoding];

This is the code i am using atm. The problem is that both string1 and string2 appear to be ok but the string named body appears to not working.. :< Any help ?

  • Can you give a sample of the output e.g. what values you have in string1, string2 and body? – Tomas McGuinness Oct 09 '12 at 09:10
  • I have coordinates that i get from an updatelocation method. e.g 37.785834. I get two of these that i want to join them in a string of this format geoX#string1#geoY#string2 so my rest service can accept them. – Dimitris Koumouras Oct 09 '12 at 09:13

3 Answers3

1

body is not an NSString instance here, but NSData (because you're using `dataUsingEncoding:". If you want to see concatenation of stings in system log you should write something like that:

NSString* bodyString = [NSString stringWithFormat:@"geoX#%@#geoY#%@", string1, string2];
NSData* bodyData = [bodyString dataUsingEncoding:NSUTF8StringEncoding];

and then you can NSLog(@"Body: %@", bodyString); to see it's contents and then use bodyData for making http request.

DrummerB
  • 39,814
  • 12
  • 105
  • 142
dieworld
  • 1,090
  • 9
  • 9
  • Really thanks all. Idd body was NSData and not NSString although i had it in front of my eyes i was really confused. This is working great now thanks! – Dimitris Koumouras Oct 09 '12 at 09:21
1

body is not an NSString; it is an NSData because of your call to dataUsingEncoding.

neilvillareal
  • 3,935
  • 1
  • 26
  • 27
1

I believe this is happening because you are just logging the raw data. Try creating a string from the data and then logging it like this:

body = [[NSString stringWithFormat:@"geoX#%@#geoY#%@", string1, string2] dataUsingEncoding:NSUTF8StringEncoding];
NSString *string = [[NSString alloc] initWithData:body encoding:NSUTF8StringEncoding];
NSLog(@"%@",string);
Mick MacCallum
  • 129,200
  • 40
  • 280
  • 281