Search code examples
objective-ciosnsstringnsstringencoding

Converting two double values to String and then making a string using the new strings


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 ?


Solution

  • 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.